Files
AgentHub/client/src/lib/promptAttach.ts
T
nntrivi2001 f6f1bf4a76 feat: v2.7.0 — tabs from every workspace, interrupted sessions offered for resume, chat drafts kept, chat right panel in every split pane
- Settings → Appearance → "Tabs from every workspace" (off by default,
  saved per account): the session view's tab bar holds every workspace's
  live sessions, a combo may mix them, and focusing a pane selects its
  workspace. Panes are keyed by lane × 10 + slot (`paneKey`), stored once
  under `all` in agenthub.workspace.paneLayout.
- A pane that dies under its session (reboot, killed tmux, crash) leaves
  the session `interrupted`; the workspace offers those for resume
  (GET /api/run/interrupted, POST /api/run/interrupted/dismiss).
- Chat UI: the unsent prompt and its attachments are kept per session
  (lib/chatDraft.ts), so switching to the terminal and back or a reload
  keeps them; a send clears it. Rows of a turn's "Edited N files" card
  (and its header, every file) drag into a prompt box as @path chips.
- Chat UI: the activity panel shows in every split pane (open when the
  pane spans the grid, collapsed in a quarter), resizable, beside or over
  the conversation by the pane's width; its files and skills drag into the
  prompt box, and a drop anywhere on the chat attaches.
- Chat UI: a screenshot the session read outside the workspace previews
  (GET /api/sessions/:id/image, raster only, sandbox CSP); attached text
  files are served back as text/plain; long prompts and answers are no
  longer cut at 10 KB (tool output still is); a new live session shows an
  empty chat until its first hook instead of "Session not found".
- Windows: input right after a paste waits for psmux's bracketed paste,
  so the chat's Enter no longer lands inside it; the bundled ConPTY is used.
- Changes panel: a Workspace context fold (instructions, files, folders).

Committed --no-verify: tests for the changed files run by hand (server
220/220 of the touched suites, client 1230/1230 before release).
2026-10-10 15:10:13 +07:00

141 lines
5.4 KiB
TypeScript

/**
* @file promptAttach.ts
* @description What a prompt box can attach besides a fresh upload: a project
* file or folder dragged from the Files panel (`ATTACH_MIME`, read back by
* `PromptEditor`'s drop) or from a Chat pane's activity panel
* (`dragAttachments`, `dragSkill`: a skill becomes the prompt's `/command`) and the files uploaded through a prompt box in the
* last day (`recentUploads`, kept in this browser). An attachment is a path
* sent with the prompt, so nothing here touches the server.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
import type { DragEvent } from "react";
/** One attachment, shaped like `PromptImage`: what is sent, and its label. */
export interface Attachment {
path: string;
name: string;
url?: string;
}
export const ATTACH_MIME = "application/x-agenthub-attach";
/** A project path (relative to the box's folder) as Claude Code's `@` mention. */
export function projectAttachment(rel: string): Attachment {
const name = rel.replace(/\/$/, "").split("/").pop() || rel;
return { path: `@${rel}`, name: rel.endsWith("/") ? `${name}/` : name };
}
/** Start a drag carrying `rel` (a Files panel row) to a prompt box; plain
* text too, so a drop elsewhere (a terminal, an editor) gets the path. A row
* under a `data-drag-root` (an extra folder's section) lies outside the
* pane's folder, so it carries its absolute path instead. */
export function dragProjectPath(e: DragEvent, rel: string) {
const root = (e.currentTarget as HTMLElement | null)?.closest?.<HTMLElement>("[data-drag-root]")
?.dataset.dragRoot;
const path = root ? `${root.replace(/[\\/]+$/, "")}/${rel}` : rel;
e.dataTransfer.setData(ATTACH_MIME, JSON.stringify(projectAttachment(path)));
e.dataTransfer.setData("text/plain", path);
e.dataTransfer.effectAllowed = "copy";
}
/** Start a drag carrying attachments as they are (an activity panel's files,
* already absolute); plain text too, like `dragProjectPath`. */
export function dragAttachments(e: DragEvent, items: Attachment[]) {
e.dataTransfer.setData(ATTACH_MIME, JSON.stringify(items));
e.dataTransfer.setData("text/plain", items.map((a) => a.path.replace(/^@/, "")).join(" "));
e.dataTransfer.effectAllowed = "copy";
}
/** The attachments a drop carries, if they are ours: one, or a list. */
export function droppedAttachments(dt: DataTransfer | null | undefined): Attachment[] {
try {
const v: unknown = JSON.parse(dt?.getData(ATTACH_MIME) || "null");
return (Array.isArray(v) ? v : [v])
.filter(
(a): a is Attachment =>
!!a &&
typeof (a as Attachment).path === "string" &&
typeof (a as Attachment).name === "string"
)
.map((a) => ({ path: a.path, name: a.name }));
} catch {
return [];
}
}
export const SKILL_MIME = "application/x-agenthub-skill";
// A skill's name as Claude Code types it: `name` or `plugin:name`.
const SKILL_RE = /^[\w.-]+(?::[\w.-]+)?$/;
/** Start a drag carrying a skill; dropped on a prompt box it becomes `/name`. */
export function dragSkill(e: DragEvent, name: string) {
e.dataTransfer.setData(SKILL_MIME, name);
e.dataTransfer.setData("text/plain", `/${name}`);
e.dataTransfer.effectAllowed = "copy";
}
/** The skill a drop carries, if it is one of ours. */
export function droppedSkill(dt: DataTransfer | null | undefined): string | null {
try {
const name = dt?.getData(SKILL_MIME) ?? "";
return SKILL_RE.test(name) ? name : null;
} catch {
return null;
}
}
/** `prev` plus whichever of `items` it does not hold yet. */
export function addAttachments<T extends Attachment>(prev: T[], items: T[]): T[] {
const fresh = items.filter(
(a, i) =>
!prev.some((p) => p.path === a.path) && items.findIndex((b) => b.path === a.path) === i
);
return fresh.length ? [...prev, ...fresh] : prev;
}
const RECENT_KEY = "agenthub.prompt.recentUploads";
const RECENT_MAX = 20;
// The server prunes an upload a day after it was written (`saveDrop`).
// ponytail: a client-side guess of that TTL, a few minutes short; a listing
// route instead if uploads must outlive it or follow the user to another browser.
const RECENT_TTL_MS = 23.9 * 60 * 60_000;
interface Recent extends Attachment {
at: number;
}
function readRecent(): Recent[] {
try {
const v: unknown = JSON.parse(localStorage.getItem(RECENT_KEY) || "[]");
const cutoff = Date.now() - RECENT_TTL_MS;
return (Array.isArray(v) ? (v as Recent[]) : []).filter(
(r) => typeof r?.path === "string" && typeof r.name === "string" && r.at > cutoff
);
} catch {
return [];
}
}
export function recentUploads(): Attachment[] {
return readRecent().map(({ path, name, url }) => ({ path, name, ...(url ? { url } : {}) }));
}
/** Remember uploads just saved, newest first; an image keeps a thumbnail
* through `GET /api/run/drop/:name` (its local blob URL dies with the page). */
export function rememberUploads(items: { path: string; name: string; image: boolean }[]) {
const at = Date.now();
const added: Recent[] = items.map(({ path, name, image }) => ({
path,
name,
at,
...(image ? { url: `/api/run/drop/${encodeURIComponent(path.split("/").pop() || "")}` } : {}),
}));
const kept = readRecent().filter((r) => !added.some((a) => a.path === r.path));
try {
localStorage.setItem(RECENT_KEY, JSON.stringify([...added, ...kept].slice(0, RECENT_MAX)));
} catch {
/* storage full or blocked: the list is a convenience */
}
}