feat: better context files (#222)

feat: context files
This commit is contained in:
Carlos Florêncio
2026-02-14 20:13:11 +00:00
committed by GitHub
parent ee612a42c5
commit f4d1dfd3a7
49 changed files with 3809 additions and 801 deletions
@@ -0,0 +1,39 @@
'use client';
import { memo } from 'react';
import type { CommentContextItem } from '@/lib/types/context';
import { getFileName } from '@/lib/utils/file-path';
import { ContextChip } from './context-chip';
import { CommentDisplay } from '@/components/diff/comment-display';
export const CommentItem = memo(function CommentItem({ item }: { item: CommentContextItem }) {
const fileName = getFileName(item.filePath);
const preview = (
<div className="space-y-1.5">
<div className="text-xs font-medium text-muted-foreground truncate" title={item.filePath}>
{fileName}
</div>
<div className="space-y-1">
{item.comments.map((comment) => (
<CommentDisplay
key={comment.id}
comment={comment}
compact
onDelete={() => item.onRemoveComment(comment.id)}
/>
))}
</div>
</div>
);
return (
<ContextChip
kind="comment"
label={item.label}
preview={preview}
onClick={item.onOpen}
onRemove={item.onRemove}
/>
);
});
@@ -0,0 +1,131 @@
'use client';
import { memo, useRef, useState, type ReactNode } from 'react';
import {
IconListCheck,
IconFile,
IconMessageDots,
IconPhoto,
IconAt,
IconX,
IconPinFilled,
} from '@tabler/icons-react';
import type { TablerIcon } from '@tabler/icons-react';
import { HoverCard, HoverCardTrigger, HoverCardContent } from '@kandev/ui/hover-card';
import type { ContextItemKind } from '@/lib/types/context';
const ICON_BY_KIND: Record<ContextItemKind, TablerIcon> = {
plan: IconListCheck,
file: IconFile,
comment: IconMessageDots,
'plan-comment': IconMessageDots,
image: IconPhoto,
prompt: IconAt,
};
type ContextChipProps = {
kind: ContextItemKind;
label: string;
pinned?: boolean;
preview?: ReactNode;
/** Data URL to render as a tiny thumbnail instead of the default icon */
thumbnail?: string;
onClick?: () => void;
onUnpin?: () => void;
onRemove?: () => void;
};
export const ContextChip = memo(function ContextChip({
kind,
label,
pinned,
preview,
thumbnail,
onClick,
onUnpin,
onRemove,
}: ContextChipProps) {
const Icon = ICON_BY_KIND[kind];
const chip = (
<div
className={`group flex items-center gap-1 px-2 py-0.5 text-xs text-muted-foreground bg-muted/50 rounded border border-border/50 ${onClick ? 'cursor-pointer hover:bg-muted/80' : ''}`}
onClick={onClick}
>
{thumbnail ? (
// eslint-disable-next-line @next/next/no-img-element -- base64 thumbnail
<img src={thumbnail} alt="" className="h-3 w-3 shrink-0 rounded-sm object-cover" />
) : (
<Icon className="h-3 w-3 shrink-0" />
)}
<span className="truncate max-w-[120px]">{label}</span>
{pinned && onUnpin && (
<button
onClick={(e) => {
e.stopPropagation();
onUnpin();
}}
className="ml-0.5 text-muted-foreground/70 hover:text-foreground cursor-pointer"
title="Unpin (will be removed after send)"
>
<IconPinFilled className="h-2.5 w-2.5" />
</button>
)}
{onRemove && (
<button
onClick={(e) => {
e.stopPropagation();
onRemove();
}}
className="opacity-0 group-hover:opacity-100 ml-0.5 hover:text-foreground cursor-pointer"
>
<IconX className="h-2.5 w-2.5" />
</button>
)}
</div>
);
if (!preview) return chip;
return (
<ControlledHoverChip preview={preview}>
{chip}
</ControlledHoverChip>
);
});
function ControlledHoverChip({
preview,
children,
}: {
preview: ReactNode;
children: ReactNode;
}) {
const [open, setOpen] = useState(false);
const suppressRef = useRef(false);
return (
<HoverCard
open={open}
onOpenChange={(next) => {
if (next && suppressRef.current) return;
setOpen(next);
}}
openDelay={300}
>
<HoverCardTrigger
asChild
onClick={() => {
suppressRef.current = true;
setOpen(false);
setTimeout(() => { suppressRef.current = false; }, 300);
}}
>
{children}
</HoverCardTrigger>
<HoverCardContent side="top" align="start" className="w-80 max-h-80 overflow-y-auto">
{preview}
</HoverCardContent>
</HoverCard>
);
}
@@ -0,0 +1,32 @@
'use client';
import type { ContextItem } from '@/lib/types/context';
import { PlanItem } from './plan-item';
import { FileItem } from './file-item';
import { PromptItem } from './prompt-item';
import { CommentItem } from './comment-item';
import { PlanCommentItem } from './plan-comment-item';
import { ImageItem } from './image-item';
export function ContextItemRenderer({
item,
sessionId,
}: {
item: ContextItem;
sessionId?: string | null;
}) {
switch (item.kind) {
case 'plan':
return <PlanItem item={item} />;
case 'file':
return <FileItem item={item} sessionId={sessionId} />;
case 'prompt':
return <PromptItem item={item} />;
case 'comment':
return <CommentItem item={item} />;
case 'plan-comment':
return <PlanCommentItem item={item} />;
case 'image':
return <ImageItem item={item} />;
}
}
@@ -0,0 +1,41 @@
'use client';
import type { ReactNode } from 'react';
import type { ContextItem } from '@/lib/types/context';
import { ContextItemRenderer } from './context-item-renderer';
type ContextZoneProps = {
items: ContextItem[];
sessionId?: string | null;
queueSlot?: ReactNode;
todoSlot?: ReactNode;
clarificationSlot?: ReactNode;
};
export function ContextZone({
items,
sessionId,
queueSlot,
todoSlot,
clarificationSlot,
}: ContextZoneProps) {
const hasContent = !!queueSlot || !!todoSlot || items.length > 0 || !!clarificationSlot;
if (!hasContent) return null;
return (
<div className="overflow-y-auto max-h-[40%] border-b border-border/50 shrink-0">
<div className="px-2 pt-2 pb-1 space-y-1.5">
{queueSlot}
{todoSlot}
{items.length > 0 && (
<div className="flex items-center gap-1 flex-wrap px-0 py-0.5">
{items.map((item) => (
<ContextItemRenderer key={item.id} item={item} sessionId={sessionId} />
))}
</div>
)}
{clarificationSlot}
</div>
</div>
);
}
@@ -0,0 +1,30 @@
'use client';
import { memo, useCallback } from 'react';
import type { FileContextItem } from '@/lib/types/context';
import { ContextChip } from './context-chip';
import { LazyFilePreview } from './lazy-file-preview';
export const FileItem = memo(function FileItem({
item,
sessionId,
}: {
item: FileContextItem;
sessionId?: string | null;
}) {
const handleClick = useCallback(() => {
item.onOpen(item.path);
}, [item]);
return (
<ContextChip
kind="file"
label={item.label}
pinned={item.pinned}
preview={<LazyFilePreview path={item.path} sessionId={sessionId ?? null} />}
onClick={handleClick}
onUnpin={item.onUnpin}
onRemove={item.onRemove}
/>
);
});
@@ -0,0 +1,56 @@
'use client';
import { memo, useState, useCallback } from 'react';
import {
Dialog,
DialogContent,
DialogTitle,
} from '@kandev/ui/dialog';
import type { ImageContextItem } from '@/lib/types/context';
import { ContextChip } from './context-chip';
export const ImageItem = memo(function ImageItem({ item }: { item: ImageContextItem }) {
const [dialogOpen, setDialogOpen] = useState(false);
const handleClick = useCallback(() => {
setDialogOpen(true);
}, []);
const preview = (
<div className="space-y-1.5">
{/* eslint-disable-next-line @next/next/no-img-element -- base64 preview */}
<img
src={item.attachment.preview}
alt="Preview"
className="max-w-full max-h-48 rounded object-contain"
/>
<div className="text-xs text-muted-foreground">
{item.attachment.width} &times; {item.attachment.height}
</div>
</div>
);
return (
<>
<ContextChip
kind="image"
label={item.label}
thumbnail={item.attachment.preview}
preview={preview}
onClick={handleClick}
onRemove={item.onRemove}
/>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent className="max-w-3xl p-2">
<DialogTitle className="sr-only">Image preview</DialogTitle>
{/* eslint-disable-next-line @next/next/no-img-element -- base64 preview */}
<img
src={item.attachment.preview}
alt="Full size preview"
className="max-w-full max-h-[80vh] rounded object-contain mx-auto"
/>
</DialogContent>
</Dialog>
</>
);
});
@@ -0,0 +1,10 @@
export { ContextChip } from './context-chip';
export { ContextItemRenderer } from './context-item-renderer';
export { ContextZone } from './context-zone';
export { PlanItem } from './plan-item';
export { FileItem } from './file-item';
export { PromptItem } from './prompt-item';
export { CommentItem } from './comment-item';
export { PlanCommentItem } from './plan-comment-item';
export { ImageItem } from './image-item';
export { LazyFilePreview } from './lazy-file-preview';
@@ -0,0 +1,72 @@
'use client';
import { useState, useEffect } from 'react';
import { getWebSocketClient } from '@/lib/ws/connection';
import { requestFileContent } from '@/lib/ws/workspace-files';
type LazyFilePreviewProps = {
path: string;
sessionId: string | null;
};
export function LazyFilePreview({ path, sessionId }: LazyFilePreviewProps) {
const [content, setContent] = useState<string | null>(null);
const [error, setError] = useState(false);
const [loading, setLoading] = useState(true);
useEffect(() => {
if (!sessionId) {
setLoading(false);
setError(true);
return;
}
let cancelled = false;
async function fetch() {
try {
const client = getWebSocketClient();
if (!client) {
setError(true);
return;
}
const response = await requestFileContent(client, sessionId!, path);
if (!cancelled) {
setContent(response.content ?? null);
}
} catch {
if (!cancelled) setError(true);
} finally {
if (!cancelled) setLoading(false);
}
}
fetch();
return () => { cancelled = true; };
}, [path, sessionId]);
if (loading) {
return (
<div className="space-y-1.5">
<div className="text-muted-foreground text-xs font-medium truncate">{path}</div>
<div className="h-3 w-3/4 bg-muted animate-pulse rounded" />
<div className="h-3 w-1/2 bg-muted animate-pulse rounded" />
</div>
);
}
if (error || content === null) {
return <div className="text-muted-foreground text-xs">{path}</div>;
}
const preview = content.length > 2000 ? content.slice(0, 2000) + '...' : content;
return (
<div className="space-y-1.5">
<div className="text-muted-foreground text-xs font-medium truncate">{path}</div>
<pre className="text-[10px] leading-tight font-mono whitespace-pre-wrap break-all">
{preview}
</pre>
</div>
);
}
@@ -0,0 +1,65 @@
'use client';
import { useEffect } from 'react';
import { useAppStore, useAppStoreApi } from '@/components/state-provider';
import { getTaskPlan } from '@/lib/api/domains/plan-api';
type LazyPlanPreviewProps = {
taskId: string | null;
};
export function LazyPlanPreview({ taskId }: LazyPlanPreviewProps) {
const plan = useAppStore((state) =>
taskId ? state.taskPlans.byTaskId[taskId] ?? null : null
);
const loaded = useAppStore((state) =>
taskId ? state.taskPlans.loadedByTaskId[taskId] ?? false : false
);
const loading = useAppStore((state) =>
taskId ? state.taskPlans.loadingByTaskId[taskId] ?? false : false
);
const storeApi = useAppStoreApi();
useEffect(() => {
if (!taskId || loaded || loading) return;
const { setTaskPlanLoading } = storeApi.getState();
setTaskPlanLoading(taskId, true);
getTaskPlan(taskId).then((result) => {
storeApi.getState().setTaskPlan(taskId, result);
}).catch(() => {
storeApi.getState().setTaskPlanLoading(taskId, false);
});
}, [taskId, loaded, loading, storeApi]);
if (!taskId) {
return <div className="text-xs text-muted-foreground">No task selected</div>;
}
if (loading || !loaded) {
return (
<div className="space-y-1.5">
<div className="text-muted-foreground text-xs font-medium">Plan</div>
<div className="h-3 w-3/4 bg-muted animate-pulse rounded" />
<div className="h-3 w-1/2 bg-muted animate-pulse rounded" />
</div>
);
}
if (!plan?.content) {
return <div className="text-xs text-muted-foreground">Plan is empty</div>;
}
const preview = plan.content.length > 2000 ? plan.content.slice(0, 2000) + '...' : plan.content;
return (
<div className="space-y-1.5">
<div className="text-muted-foreground text-xs font-medium">Plan</div>
<pre className="text-[10px] leading-tight font-mono whitespace-pre-wrap break-all">
{preview}
</pre>
</div>
);
}
@@ -0,0 +1,32 @@
'use client';
import { memo } from 'react';
import type { PlanCommentContextItem } from '@/lib/types/context';
import { ContextChip } from './context-chip';
export const PlanCommentItem = memo(function PlanCommentItem({ item }: { item: PlanCommentContextItem }) {
const preview = (
<div className="space-y-1.5">
{item.comments.map((comment) => (
<div key={comment.id} className="text-xs space-y-0.5">
{comment.selectedText && (
<div className="text-muted-foreground italic line-clamp-2">
&ldquo;{comment.selectedText}&rdquo;
</div>
)}
<div className="break-words">{comment.comment}</div>
</div>
))}
</div>
);
return (
<ContextChip
kind="plan-comment"
label={item.label}
preview={preview}
onClick={item.onOpen}
onRemove={item.onRemove}
/>
);
});
@@ -0,0 +1,20 @@
'use client';
import { memo } from 'react';
import type { PlanContextItem } from '@/lib/types/context';
import { ContextChip } from './context-chip';
import { LazyPlanPreview } from './lazy-plan-preview';
export const PlanItem = memo(function PlanItem({ item }: { item: PlanContextItem }) {
return (
<ContextChip
kind="plan"
label={item.label}
pinned={item.pinned}
preview={<LazyPlanPreview taskId={item.taskId ?? null} />}
onClick={item.onOpen}
onUnpin={item.onUnpin}
onRemove={item.onRemove}
/>
);
});
@@ -0,0 +1,20 @@
'use client';
import { memo } from 'react';
import type { PromptContextItem } from '@/lib/types/context';
import { ContextChip } from './context-chip';
import { PromptPreview } from './prompt-preview';
export const PromptItem = memo(function PromptItem({ item }: { item: PromptContextItem }) {
return (
<ContextChip
kind="prompt"
label={item.label}
pinned={item.pinned}
preview={<PromptPreview content={item.promptContent ?? null} />}
onClick={item.onClick}
onUnpin={item.onUnpin}
onRemove={item.onRemove}
/>
);
});
@@ -0,0 +1,37 @@
'use client';
import { useAppStore } from '@/components/state-provider';
type PromptPreviewProps = {
content: string | null;
};
export function PromptPreview({ content }: PromptPreviewProps) {
if (!content) {
return <div className="text-xs text-muted-foreground">Custom prompt</div>;
}
const truncated = content.length > 2000 ? content.slice(0, 2000) + '...' : content;
return (
<div className="space-y-1.5">
<div className="text-muted-foreground text-xs font-medium">Prompt</div>
<pre className="text-[10px] leading-tight font-mono whitespace-pre-wrap break-all">
{truncated}
</pre>
</div>
);
}
type PromptPreviewFromStoreProps = {
promptId: string;
};
export function PromptPreviewFromStore({ promptId }: PromptPreviewFromStoreProps) {
const content = useAppStore((state) => {
const prompt = state.prompts.items.find((p) => p.id === promptId);
return prompt?.content ?? null;
});
return <PromptPreview content={content} />;
}