mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-16 15:15:35 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Task comments can contain references to files in project and execution workspaces. > - Paperclip detected path-shaped inline code and showed it as an actionable file chip. > - The UI did not first confirm that the current board session could open the file. > - Missing, denied, ambiguous, remote, and unsupported files therefore looked actionable and failed after a click. > - This pull request adds an issue-scoped availability check and promotes only confirmed files to chips. > - The benefit is that the task thread shows a file action only when that action can succeed. ## Linked Issues or Issue Description **What happened?** Task comments promoted path-shaped inline code to file chips before Paperclip checked the file. A chip could point to a missing, denied, ambiguous, remote, or non-previewable file. The action then failed after the user selected it. **Expected behavior** Paperclip must show a file chip only after the server confirms that the current board session can open the exact file reference. All other path-shaped text must stay ordinary inline code. **Steps to reproduce** 1. Add a task comment that contains inline code with a missing or inaccessible workspace path. 2. Open the task thread as a board user. 3. Observe that the path looks like an actionable file chip. 4. Select the chip and observe that the file cannot open. **Paperclip version or commit** `19be4cf927` and earlier. **Deployment mode** Local dev and self-hosted server. **Access context** Board user. ## What Changed - Added shared request, response, and validation contracts for batched workspace-file availability checks. - Added an issue-scoped server endpoint that resolves file references with company, issue, workspace, and preview-access checks. - Added bounded batch concurrency and tests for missing, denied, ambiguous, remote, unsupported, and available files. - Added an issue-scoped UI availability registry that deduplicates, batches, caches, and invalidates file checks. - Changed task-comment markdown rendering so only confirmed files get chip styling and file-viewer behavior. - Bound each chip to the exact workspace target that passed the availability check. ## Verification - `pnpm exec vitest run packages/shared/src/workspace-file-resource.test.ts server/src/__tests__/file-resources.test.ts ui/src/components/MarkdownBody.test.tsx ui/src/components/WorkspaceFileMarkdownBody.availability.test.tsx ui/src/lib/remark-workspace-file-refs.test.ts ui/src/lib/workspace-file-availability.test.ts` — 93 passed, 35 skipped. - `pnpm check:token-gates` — clean. - `pnpm -r typecheck` — passed. - `pnpm build` — passed. - `pnpm test:run` — all server and UI groups passed. One unchanged CLI test saw the run-injected static AWS credentials and expected only its local `AWS_PROFILE`. The same test passed, 8 of 8, after removing only `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` from its process environment. ## Risks - File chips now appear after an asynchronous availability check, so path-shaped text can briefly render as inline code. - Availability results use the existing 30-second file-resource cache window. File-resource invalidation forces a new check. - The endpoint limits each request to 100 references and the client chunks larger sets. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex with GPT-5. The service did not expose a more specific model ID or context-window size. The agent used high-reasoning mode, repository tools, command execution, and test execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
350 lines
12 KiB
TypeScript
350 lines
12 KiB
TypeScript
import { createContext, useContext, useCallback, useMemo, type ReactNode } from "react";
|
|
import { useLocation, useNavigate, type NavigateOptions } from "@/lib/router";
|
|
import type { WorkspaceFileSelector } from "@paperclipai/shared";
|
|
import type { ParsedWorkspaceFileRef } from "@/lib/workspace-file-parser";
|
|
import {
|
|
useWorkspaceFileAvailability,
|
|
type WorkspaceFileAvailabilityRegistry,
|
|
} from "@/hooks/useWorkspaceFileAvailability";
|
|
|
|
export interface FileViewerUrlState {
|
|
path: string;
|
|
line: number | null;
|
|
column: number | null;
|
|
workspace: WorkspaceFileSelector;
|
|
projectId: string | null;
|
|
workspaceId: string | null;
|
|
}
|
|
|
|
export interface FileViewerContextValue {
|
|
issueId: string;
|
|
/**
|
|
* Batched preflight registry. Auto-detected markdown file references consult
|
|
* it so a chip only renders once this issue's session can actually open the
|
|
* resolved target.
|
|
*/
|
|
availability: WorkspaceFileAvailabilityRegistry;
|
|
/** Current viewer state derived from the URL, or null if closed. */
|
|
state: FileViewerUrlState | null;
|
|
/** True when the sheet is in browse mode (URL carries `browse=1`). */
|
|
browse: boolean;
|
|
/** The active browse search query (URL `q`), or null. */
|
|
query: string | null;
|
|
browseProjectId: string | null;
|
|
browseWorkspaceId: string | null;
|
|
folderPath: string | null;
|
|
open(
|
|
ref: Pick<ParsedWorkspaceFileRef, "path" | "line" | "column" | "projectId" | "workspaceId"> & {
|
|
workspace?: WorkspaceFileSelector;
|
|
},
|
|
opts?: {
|
|
fromBrowse?: boolean;
|
|
browseState?: Partial<FileViewerBrowseState>;
|
|
},
|
|
): void;
|
|
/** Open (or stay in) browse mode, optionally seeding the search query. */
|
|
openBrowse(opts?: { q?: string }): void;
|
|
/** Update URL-backed browse state without closing the active preview. */
|
|
updateBrowseState(opts: Partial<FileViewerBrowseState>): void;
|
|
openFolder(
|
|
ref: Pick<ParsedWorkspaceFileRef, "path" | "projectId" | "workspaceId"> & {
|
|
workspace?: WorkspaceFileSelector;
|
|
},
|
|
): void;
|
|
/** From a file opened via browse, return to the browse list. */
|
|
backToFiles(): void;
|
|
close(): void;
|
|
}
|
|
|
|
const FileViewerContext = createContext<FileViewerContextValue | null>(null);
|
|
|
|
export const FILE_VIEWER_NAVIGATE_OPTIONS = {
|
|
replace: false,
|
|
preventScrollReset: true,
|
|
} satisfies NavigateOptions;
|
|
|
|
export function getCurrentFileViewerSearch(fallbackSearch: string): string {
|
|
if (typeof window === "undefined") return fallbackSearch;
|
|
return window.location.search;
|
|
}
|
|
|
|
export function shouldNavigateFileViewerSearch(nextSearch: string, fallbackSearch: string): boolean {
|
|
return nextSearch !== getCurrentFileViewerSearch(fallbackSearch);
|
|
}
|
|
|
|
export function readFileViewerStateFromSearch(search: string): FileViewerUrlState | null {
|
|
const params = new URLSearchParams(search);
|
|
const path = params.get("file");
|
|
if (!path) return null;
|
|
const lineRaw = params.get("line");
|
|
const columnRaw = params.get("column");
|
|
const workspaceRaw = params.get("workspace");
|
|
const projectIdRaw = params.get("projectId");
|
|
const workspaceIdRaw = params.get("workspaceId");
|
|
const hasExplicitTarget = Boolean(projectIdRaw && workspaceIdRaw);
|
|
const line = lineRaw ? Number.parseInt(lineRaw, 10) : NaN;
|
|
const column = columnRaw ? Number.parseInt(columnRaw, 10) : NaN;
|
|
const workspace = (workspaceRaw === "execution" || workspaceRaw === "project")
|
|
? workspaceRaw
|
|
: "auto";
|
|
return {
|
|
path,
|
|
line: Number.isFinite(line) && line > 0 ? line : null,
|
|
column: Number.isFinite(column) && column > 0 ? column : null,
|
|
workspace,
|
|
projectId: hasExplicitTarget ? projectIdRaw : null,
|
|
workspaceId: hasExplicitTarget ? workspaceIdRaw : null,
|
|
};
|
|
}
|
|
|
|
export function writeFileViewerStateToSearch(current: string, next: FileViewerUrlState | null): string {
|
|
const params = new URLSearchParams(current);
|
|
// A direct file open/close is never a browse origin — clear browse params too.
|
|
params.delete("browse");
|
|
params.delete("q");
|
|
params.delete("folder");
|
|
if (!next) {
|
|
params.delete("file");
|
|
params.delete("line");
|
|
params.delete("column");
|
|
params.delete("workspace");
|
|
params.delete("projectId");
|
|
params.delete("workspaceId");
|
|
} else {
|
|
params.set("file", next.path);
|
|
if (next.line !== null) params.set("line", String(next.line));
|
|
else params.delete("line");
|
|
if (next.column !== null) params.set("column", String(next.column));
|
|
else params.delete("column");
|
|
if (next.workspace && next.workspace !== "auto") params.set("workspace", next.workspace);
|
|
else params.delete("workspace");
|
|
if (next.projectId) params.set("projectId", next.projectId);
|
|
else params.delete("projectId");
|
|
if (next.workspaceId) params.set("workspaceId", next.workspaceId);
|
|
else params.delete("workspaceId");
|
|
}
|
|
const str = params.toString();
|
|
return str ? `?${str}` : "";
|
|
}
|
|
|
|
export interface FileViewerBrowseState {
|
|
q: string | null;
|
|
folderPath: string | null;
|
|
projectId: string | null;
|
|
workspaceId: string | null;
|
|
}
|
|
|
|
export function readBrowseStateFromSearch(search: string): FileViewerBrowseState | null {
|
|
const params = new URLSearchParams(search);
|
|
if (params.get("browse") !== "1") return null;
|
|
const q = params.get("q");
|
|
const folder = params.get("folder");
|
|
const projectId = params.get("projectId");
|
|
const workspaceId = params.get("workspaceId");
|
|
return {
|
|
q: q && q.length > 0 ? q : null,
|
|
folderPath: folder && folder.length > 0 ? folder : null,
|
|
projectId: projectId || null,
|
|
workspaceId: workspaceId || null,
|
|
};
|
|
}
|
|
|
|
export function writeFolderViewerStateToSearch(
|
|
current: string,
|
|
next: {
|
|
path: string;
|
|
projectId: string | null;
|
|
workspaceId: string | null;
|
|
},
|
|
): string {
|
|
const params = new URLSearchParams(current);
|
|
params.delete("file");
|
|
params.delete("line");
|
|
params.delete("column");
|
|
params.delete("workspace");
|
|
params.delete("q");
|
|
params.set("browse", "1");
|
|
params.set("folder", next.path);
|
|
if (next.projectId) params.set("projectId", next.projectId);
|
|
else params.delete("projectId");
|
|
if (next.workspaceId) params.set("workspaceId", next.workspaceId);
|
|
else params.delete("workspaceId");
|
|
const str = params.toString();
|
|
return str ? `?${str}` : "";
|
|
}
|
|
|
|
export function writeBrowseStateToSearch(current: string, next: Partial<FileViewerBrowseState>): string {
|
|
const params = new URLSearchParams(current);
|
|
params.set("browse", "1");
|
|
if ("q" in next) {
|
|
const q = next.q?.trim() ?? "";
|
|
if (q) params.set("q", q);
|
|
else params.delete("q");
|
|
}
|
|
if ("folderPath" in next) {
|
|
const folderPath = next.folderPath?.trim() ?? "";
|
|
if (folderPath) params.set("folder", folderPath);
|
|
else params.delete("folder");
|
|
}
|
|
if ("projectId" in next) {
|
|
if (next.projectId) params.set("projectId", next.projectId);
|
|
else params.delete("projectId");
|
|
}
|
|
if ("workspaceId" in next) {
|
|
if (next.workspaceId) params.set("workspaceId", next.workspaceId);
|
|
else params.delete("workspaceId");
|
|
}
|
|
const str = params.toString();
|
|
return str ? `?${str}` : "";
|
|
}
|
|
|
|
interface FileViewerProviderProps {
|
|
issueId: string;
|
|
children: ReactNode;
|
|
enabled?: boolean;
|
|
}
|
|
|
|
export function FileViewerProvider({ issueId, children, enabled = true }: FileViewerProviderProps) {
|
|
if (!enabled) return <>{children}</>;
|
|
return <EnabledFileViewerProvider issueId={issueId}>{children}</EnabledFileViewerProvider>;
|
|
}
|
|
|
|
function EnabledFileViewerProvider({ issueId, children }: Omit<FileViewerProviderProps, "enabled">) {
|
|
const location = useLocation();
|
|
const navigate = useNavigate();
|
|
const state = useMemo(() => readFileViewerStateFromSearch(location.search), [location.search]);
|
|
const browseState = useMemo(() => readBrowseStateFromSearch(location.search), [location.search]);
|
|
const availability = useWorkspaceFileAvailability(issueId);
|
|
|
|
const navigateSearch = useCallback(
|
|
(nextSearch: string, opts?: Partial<NavigateOptions>) => {
|
|
if (!shouldNavigateFileViewerSearch(nextSearch, location.search)) return;
|
|
navigate(
|
|
{ pathname: location.pathname, hash: location.hash, search: nextSearch },
|
|
{ ...FILE_VIEWER_NAVIGATE_OPTIONS, ...opts, state: location.state },
|
|
);
|
|
},
|
|
[location.hash, location.pathname, location.state, navigate],
|
|
);
|
|
|
|
const open = useCallback<FileViewerContextValue["open"]>(
|
|
(ref, opts) => {
|
|
let nextSearch = writeFileViewerStateToSearch(location.search, {
|
|
path: ref.path,
|
|
line: ref.line ?? null,
|
|
column: ref.column ?? null,
|
|
workspace: ref.workspace ?? "auto",
|
|
projectId: ref.projectId ?? null,
|
|
workspaceId: ref.workspaceId ?? null,
|
|
});
|
|
if (opts?.fromBrowse) {
|
|
const params = new URLSearchParams(nextSearch);
|
|
params.set("browse", "1");
|
|
const previousParams = new URLSearchParams(location.search);
|
|
const prevQ = previousParams.get("q");
|
|
const prevFolder = previousParams.get("folder");
|
|
const nextQ = Object.prototype.hasOwnProperty.call(opts.browseState ?? {}, "q")
|
|
? opts.browseState?.q
|
|
: prevQ;
|
|
const nextFolder = Object.prototype.hasOwnProperty.call(opts.browseState ?? {}, "folderPath")
|
|
? opts.browseState?.folderPath
|
|
: prevFolder;
|
|
if (nextQ) params.set("q", nextQ);
|
|
else params.delete("q");
|
|
if (nextFolder) params.set("folder", nextFolder);
|
|
else params.delete("folder");
|
|
nextSearch = params.toString() ? `?${params.toString()}` : "";
|
|
}
|
|
navigateSearch(nextSearch);
|
|
},
|
|
[location.search, navigateSearch],
|
|
);
|
|
|
|
const openBrowse = useCallback<FileViewerContextValue["openBrowse"]>(
|
|
(opts) => {
|
|
const params = new URLSearchParams(location.search);
|
|
params.delete("file");
|
|
params.delete("line");
|
|
params.delete("column");
|
|
params.delete("folder");
|
|
params.set("browse", "1");
|
|
if (typeof opts?.q === "string" && opts.q.length > 0) params.set("q", opts.q);
|
|
else params.delete("q");
|
|
navigateSearch(params.toString() ? `?${params.toString()}` : "");
|
|
},
|
|
[location.search, navigateSearch],
|
|
);
|
|
|
|
const updateBrowseState = useCallback<FileViewerContextValue["updateBrowseState"]>(
|
|
(opts) => {
|
|
navigateSearch(writeBrowseStateToSearch(location.search, opts), { replace: true });
|
|
},
|
|
[location.search, navigateSearch],
|
|
);
|
|
|
|
const openFolder = useCallback<FileViewerContextValue["openFolder"]>(
|
|
(ref) => {
|
|
const nextSearch = writeFolderViewerStateToSearch(location.search, {
|
|
path: ref.path,
|
|
projectId: ref.projectId ?? null,
|
|
workspaceId: ref.workspaceId ?? null,
|
|
});
|
|
navigateSearch(nextSearch);
|
|
},
|
|
[location.search, navigateSearch],
|
|
);
|
|
|
|
const backToFiles = useCallback(() => {
|
|
const params = new URLSearchParams(location.search);
|
|
params.delete("file");
|
|
params.delete("line");
|
|
params.delete("column");
|
|
params.delete("workspace");
|
|
params.set("browse", "1");
|
|
navigateSearch(params.toString() ? `?${params.toString()}` : "");
|
|
}, [location.search, navigateSearch]);
|
|
|
|
const close = useCallback(() => {
|
|
const currentSearch = getCurrentFileViewerSearch(location.search);
|
|
const params = new URLSearchParams(writeFileViewerStateToSearch(currentSearch, null).replace(/^\?/, ""));
|
|
params.delete("browse");
|
|
params.delete("q");
|
|
params.delete("folder");
|
|
navigateSearch(params.toString() ? `?${params.toString()}` : "");
|
|
}, [location.search, navigateSearch]);
|
|
|
|
const value = useMemo<FileViewerContextValue>(
|
|
() => ({
|
|
issueId,
|
|
availability,
|
|
state,
|
|
browse: browseState !== null,
|
|
query: browseState?.q ?? null,
|
|
browseProjectId: browseState?.projectId ?? null,
|
|
browseWorkspaceId: browseState?.workspaceId ?? null,
|
|
folderPath: browseState?.folderPath ?? null,
|
|
open,
|
|
openBrowse,
|
|
updateBrowseState,
|
|
openFolder,
|
|
backToFiles,
|
|
close,
|
|
}),
|
|
[issueId, availability, state, browseState, open, openBrowse, updateBrowseState, openFolder, backToFiles, close],
|
|
);
|
|
|
|
return <FileViewerContext.Provider value={value}>{children}</FileViewerContext.Provider>;
|
|
}
|
|
|
|
export function useFileViewer(): FileViewerContextValue | null {
|
|
return useContext(FileViewerContext);
|
|
}
|
|
|
|
export function useRequiredFileViewer(): FileViewerContextValue {
|
|
const ctx = useContext(FileViewerContext);
|
|
if (!ctx) {
|
|
throw new Error("useRequiredFileViewer must be used within a FileViewerProvider");
|
|
}
|
|
return ctx;
|
|
}
|