Files
kandev/apps/web/lib/kanban/map-task.ts
T
Carlos Florêncio bc218dfbbe feat: add workspace sources from files panel (#1900)
* feat: add workspace sources from files panel

* fix: preserve worktree identity during recovery

* feat: unify workspace source actions

* fix: validate recovered workspace repositories

* fix: harden workspace source filesystem boundaries

* test: split workspace repository chip coverage

* fix: restrict repository rescan recovery paths

* fix: stabilize workspace source CI checks

* test: canonicalize workspace source paths

* test: align workspace source container paths

* fix: populate direct workspace repository names

* test: preserve git fixture rewrites across restarts

* test: isolate backend git fixture config

* fix: resume remote workspace executions before creation

* fix: preserve ssh agent identity during resume

* fix: preserve SSH workspace across backend restart

* fix: simplify workspace actions trigger

* fix: refine add workspace repositories UX

* docs: narrow workspace source UX updates

* fix: avoid phantom turn on session resume

* test(web): normalize workspace source request headers

* feat(web): streamline workspace source picker

* docs: remove trailing spaces from workspace source plan

* fix: prevent resume status from creating phantom turns

* fix(web): stabilize workspace source attachment

* refactor(web): extract workspace source store contract

* fix: avoid phantom turns on idle agent resume

* fix: address backend static analysis findings

* fix: preserve agentctl readiness status value

* fix: preserve resume messages without active turns

* fix: resume agents from promoted workspace root

* chore: merge origin/main into feature/add-new-repo-folder-d97

* feat(web): explain workspace source consequences

* test(websocket): count session activity subscription
2026-07-25 21:29:53 +01:00

154 lines
5.4 KiB
TypeScript

import {
isPRReviewFromMetadata,
isIssueWatchFromMetadata,
issueFieldsFromMetadata,
} from "@/lib/metadata-utils";
import type { KanbanState } from "@/lib/state/slices/kanban/types";
import type {
ForegroundActivity,
TaskPendingAction,
TaskState,
TaskSessionState,
} from "@/lib/types/http";
type KanbanTask = KanbanState["tasks"][number];
/**
* Shape accepted by {@link toKanbanTask}. Satisfied by both the HTTP Task DTO
* (which nests repositories) and the backend's `task.updated` WebSocket
* payload (which flattens `repository_id` and uses `task_id`).
*
* The single publisher / single mapper design relies on this shape being the
* contract; anyone changing what the backend emits needs to keep it in sync,
* and the parity test in `map-task.test.ts` will catch drift.
*/
export type TaskLike = {
id?: string;
task_id?: string;
workflow_step_id?: string;
title?: string;
description?: string | null;
position?: number;
state?: TaskState;
repositories?: Array<{
id?: string;
repository_id: string;
base_branch?: string;
checkout_branch?: string;
position?: number;
}>;
workspace_folders?: Array<{
id: string;
local_path: string;
display_name: string;
position: number;
}>;
repository_id?: string;
primary_session_id?: string | null;
primary_session_state?: TaskSessionState | string | null;
primary_session_pending_action?: TaskPendingAction | null;
task_pending_action?: TaskPendingAction | null;
foreground_activity?: ForegroundActivity | null;
session_count?: number | null;
review_status?: "pending" | "approved" | "changes_requested" | "rejected" | null;
primary_executor_id?: string | null;
primary_executor_type?: string | null;
primary_executor_name?: string | null;
is_remote_executor?: boolean;
parent_id?: string | null;
updated_at?: string;
created_at?: string;
metadata?: Record<string, unknown> | null;
};
export type WorkspaceMode = "inherit_parent" | "new_workspace" | "shared_group";
export function workspaceModeFromMetadata(
metadata: TaskLike["metadata"],
): WorkspaceMode | undefined {
const workspace = metadata?.workspace;
if (!workspace || typeof workspace !== "object") return undefined;
const mode = (workspace as Record<string, unknown>).mode;
if (mode === "inherit_parent" || mode === "new_workspace" || mode === "shared_group") {
return mode;
}
return undefined;
}
function pickRepositoryId(source: TaskLike): string | undefined {
return source.repository_id ?? source.repositories?.[0]?.repository_id ?? undefined;
}
function pickId(source: TaskLike): string {
return (source.id ?? source.task_id ?? "") as string;
}
export function pickPendingAction(action: unknown): TaskPendingAction | null | undefined {
if (action === null) return null;
if (action === "clarification" || action === "permission") {
return action;
}
return undefined;
}
function pickForegroundActivity(
activity: TaskLike["foreground_activity"],
): ForegroundActivity | null | undefined {
return activity === null ? null : (activity ?? undefined);
}
type KanbanTaskRepository = NonNullable<KanbanTask["repositories"]>[number];
function pickRepositories(source: TaskLike): KanbanTaskRepository[] | undefined {
if (!source.repositories) return undefined;
return source.repositories.map((r, idx) => ({
id: r.id ?? "",
repository_id: r.repository_id,
base_branch: r.base_branch ?? "",
checkout_branch: r.checkout_branch,
position: r.position ?? idx,
}));
}
function pickWorkspaceFolders(source: TaskLike): KanbanTask["workspaceFolders"] | undefined {
return source.workspace_folders?.map((folder) => ({ ...folder }));
}
/**
* Build a canonical {@link KanbanTask} from either an HTTP DTO or a WebSocket
* payload. Both paths share this helper so a single publisher change can never
* leave them out of sync again (cf. sidebar filter regressions where the HTTP
* snapshot derived `isPRReview` but the WS handler didn't).
*/
export function toKanbanTask(source: TaskLike): KanbanTask {
return {
id: pickId(source),
workflowStepId: source.workflow_step_id ?? "",
title: source.title ?? "",
description: source.description ?? undefined,
position: source.position ?? 0,
state: source.state,
repositoryId: pickRepositoryId(source),
repositories: pickRepositories(source),
workspaceFolders: pickWorkspaceFolders(source),
primarySessionId: source.primary_session_id ?? undefined,
primarySessionState: source.primary_session_state ?? undefined,
primarySessionPendingAction: pickPendingAction(source.primary_session_pending_action),
taskPendingAction: pickPendingAction(source.task_pending_action),
foregroundActivity: pickForegroundActivity(source.foreground_activity),
sessionCount: source.session_count ?? undefined,
reviewStatus: source.review_status ?? undefined,
primaryExecutorId: source.primary_executor_id ?? undefined,
primaryExecutorType: source.primary_executor_type ?? undefined,
primaryExecutorName: source.primary_executor_name ?? undefined,
isRemoteExecutor: source.is_remote_executor ?? false,
parentTaskId: source.parent_id ?? undefined,
workspaceMode: workspaceModeFromMetadata(source.metadata),
updatedAt: source.updated_at,
createdAt: source.created_at,
isPRReview: isPRReviewFromMetadata(source.metadata),
isIssueWatch: isIssueWatchFromMetadata(source.metadata),
...issueFieldsFromMetadata(source.metadata),
} as KanbanTask;
}