Files
PaperClipAI/ui/src/hooks/useIssueExternalObjects.ts
T
DottaandPaperclip 2dbaf4a7fa External object references across issue surfaces (#8512)
## Thinking Path

> - Paperclip is the open source control plane people use to coordinate
AI agents, issues, approvals, comments, and work products.
> - The involved subsystem is issue context: markdown links, issue
properties, related work, lists, filters, inbox/sidebar status, and
plugin-provided external context.
> - The gap is that URLs to external systems currently remain mostly
plain links, so humans and agents must manually open them to understand
status, identity, and liveness.
> - This matters because external work objects such as GitHub issues and
pull requests are part of the operational state of a Paperclip company.
> - The implementation keeps core provider-neutral: shared contracts,
storage, sync, routes, and UI surfaces live in core while providers can
contribute detection and status resolution.
> - This pull request adds the external object reference foundation,
GitHub provider support, issue-surface rendering, filters,
sidebar/list/inbox signals, and test/story coverage.
> - The benefit is that linked external work becomes inspectable
Paperclip context without hardcoding every provider directly into the
UI.

## Linked Issues or Issue Description

No public GitHub issue exists for this work.

Feature request:

- Problem: URLs in Paperclip issues, comments, documents, and related
surfaces do not expose provider status or object identity inline.
- Proposed behavior: detect supported external object URLs, persist
normalized references, refresh provider status, and render concise
status-aware links across issue surfaces.
- Users affected: board users, agents, and maintainers who triage issues
containing external work links.
- Acceptance: external object references are company-scoped,
provider-extensible, visible in key issue surfaces, filterable where
relevant, and covered by focused shared/server/UI tests.

Related PR search:

- No open duplicate PRs found for `external object references`.
- Closed related prior attempt: #4556.

## What Changed

- Added shared external-object contracts, validators, status/liveness
helpers, and plugin protocol declarations.
- Added database schema and additive migrations for external objects,
source mentions, and display metadata.
- Added server services/routes for detecting, syncing, summarizing,
refreshing, and resolving external objects across issues, documents,
comments, projects, and plugins.
- Added a GitHub external-object provider plus plugin SDK authoring
docs.
- Wired UI presentation across markdown links, comments, issue chat,
documents, properties, related work, issue rows, filters, inbox/sidebar
badges, and Storybook stories.
- Rebasing cleanup: moved the branch onto current `master`, repaired
stale worktree provision config, hardened environment-sensitive
tests/mocks, and removed committed screenshot artifacts from the PR
branch to keep the reviewable file set below tool limits.

## Verification

- `pnpm exec vitest run packages/shared/src/external-objects.test.ts
server/src/__tests__/external-object-routes.test.ts
server/src/__tests__/external-objects-service.test.ts
ui/src/components/ExternalObjectPill.test.tsx
ui/src/lib/external-objects.test.ts` passed after rebasing: 5 files, 56
tests.
- Historical branch verification before this PR creation included `pnpm
test:run`, `pnpm -r typecheck`, and `pnpm build`; this PR body does not
claim those were rerun after the final rebase.

## Risks

- Medium: this adds a new cross-surface sync path on
issue/document/comment writes. The implementation uses safe sync
wrappers so external-object failures warn instead of blocking core
mutations.
- Medium: the migrations introduce new tables and indexes. They are
additive and company-scoped.
- Medium: provider-specific URL parsing can miss or misclassify edge
cases. Shared canonicalization tests and provider tests cover current
GitHub shapes.
- Low: UI badge/filter behavior could add visual noise for object-heavy
issues; component tests and Storybook stories cover the intended
surfaces.

> Roadmap checked: `ROADMAP.md` references the plugin system as the
current extension path and does not list a duplicate core feature.
Related long-range docs discuss external references, work products,
preview URLs, and plugin extension points; this PR implements the scoped
external-object reference foundation.

## Model Used

OpenAI Codex, GPT-5 coding-agent runtime, with shell and GitHub CLI tool
use. Reasoning mode: medium. Exact deployed runtime model ID and context
window were not exposed in the environment.

## 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
- [x] 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>
2026-06-23 08:27:19 -05:00

251 lines
8.6 KiB
TypeScript

import { useCallback, useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import type {
ExternalObjectMention,
ExternalObjectMentionGroup,
ExternalObjectSummary,
} from "@paperclipai/shared";
import { externalObjectsApi } from "../api/externalObjects";
import { queryKeys } from "../lib/queryKeys";
import { normalizeExternalObjectHref } from "../lib/external-object-href";
import type { MarkdownExternalReferenceMap } from "../components/MarkdownBody";
import type { ExternalObjectPillData } from "../components/ExternalObjectPill";
import { instanceSettingsApi } from "../api/instanceSettings";
export const EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE = 500;
export async function fetchIssueExternalObjectSummariesInBatches(
companyId: string,
issueIds: readonly string[],
) {
const summaries: Record<string, ExternalObjectSummary> = {};
for (let index = 0; index < issueIds.length; index += EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE) {
const batch = issueIds.slice(index, index + EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE);
const response = await externalObjectsApi.getIssueSummaries(companyId, batch);
Object.assign(summaries, response.summaries);
}
return { summaries };
}
/**
* Browser-side mention-source label. Keep in sync with the shared formatter
* without coupling this hook to the server-only URL canonicalization helpers.
*/
function formatMentionSourceLabel(mention: ExternalObjectMention): string {
switch (mention.sourceKind) {
case "title":
return "Title";
case "description":
return "Description";
case "comment":
return "Comment";
case "document":
return mention.documentKey ? `Document: ${mention.documentKey}` : "Document";
case "property":
return mention.propertyKey ? `Property: ${mention.propertyKey}` : "Property";
case "plugin":
return "Plugin";
default:
return "Source";
}
}
export interface IssueExternalObjectGroup {
pill: ExternalObjectPillData;
mentionCount: number;
sourceLabels: string[];
group: ExternalObjectMentionGroup;
}
export interface IssueExternalObjectsResult {
isEnabled: boolean;
groups: IssueExternalObjectGroup[];
/** Lookup map for `MarkdownBody`'s `externalReferences` prop. */
markdownReferences: MarkdownExternalReferenceMap;
isLoading: boolean;
isError: boolean;
refetch: () => void;
}
function useExternalObjectsFeature() {
const query = useQuery({
queryKey: queryKeys.instance.experimentalSettings,
queryFn: () => instanceSettingsApi.getExperimental(),
retry: false,
});
return {
isEnabled: query.data?.enableExternalObjects === true,
isLoaded: query.data !== undefined || query.isError,
};
}
/**
* Loads `external_objects` for an issue and produces both the per-group rows
* (used by the property panel and related-work section) and the markdown URL
* lookup map (used by inline decoration). Single source of truth so every
* surface reads from the same query result.
*/
export function useIssueExternalObjects(issueId: string | null | undefined): IssueExternalObjectsResult {
const externalObjectsFeature = useExternalObjectsFeature();
const enabled = externalObjectsFeature.isEnabled && Boolean(issueId);
const query = useQuery({
queryKey: queryKeys.externalObjects.byIssue(issueId ?? "__none__"),
queryFn: () => externalObjectsApi.listForIssue(issueId!),
enabled,
staleTime: 60_000,
});
const groups = useMemo<IssueExternalObjectGroup[]>(() => {
const data = query.data ?? [];
return data
.filter((entry): entry is ExternalObjectMentionGroup => Boolean(entry.object))
.map((entry) => {
const object = entry.object!;
const sourceLabels = entry.sourceLabels && entry.sourceLabels.length > 0
? entry.sourceLabels
: Array.from(new Set(entry.mentions.map(formatMentionSourceLabel)));
return {
group: entry,
mentionCount: entry.mentionCount ?? entry.mentions.length,
sourceLabels,
pill: {
providerKey: object.providerKey,
objectType: object.objectType,
displayKey: object.displayKey,
iconKey: object.iconKey,
statusCategory: object.statusCategory,
liveness: object.liveness,
displayTitle: object.displayTitle,
statusLabel: object.statusLabel,
statusIconKey: object.statusIconKey,
url: object.sanitizedCanonicalUrl,
},
};
});
}, [query.data]);
const markdownReferences = useMemo<MarkdownExternalReferenceMap>(() => {
const result: MarkdownExternalReferenceMap = {};
for (const { group } of groups) {
const object = group.object;
if (!object) continue;
// Index by the object's canonical URL.
const canonical = normalizeExternalObjectHref(object.sanitizedCanonicalUrl ?? null);
if (canonical) {
result[canonical] = {
providerKey: object.providerKey,
objectType: object.objectType,
displayKey: object.displayKey,
iconKey: object.iconKey,
statusCategory: object.statusCategory,
liveness: object.liveness,
statusLabel: object.statusLabel,
statusIconKey: object.statusIconKey,
displayTitle: object.displayTitle,
};
}
// Also index by every mention's sanitized display URL so user-pasted
// hrefs that differ only in case/punctuation still resolve.
for (const mention of group.mentions) {
const normalizedMention = normalizeExternalObjectHref(
mention.sanitizedDisplayUrl ?? null,
);
if (normalizedMention && !result[normalizedMention]) {
result[normalizedMention] = {
providerKey: object.providerKey,
objectType: object.objectType,
displayKey: object.displayKey,
iconKey: object.iconKey,
statusCategory: object.statusCategory,
liveness: object.liveness,
statusLabel: object.statusLabel,
statusIconKey: object.statusIconKey,
displayTitle: object.displayTitle,
};
}
}
}
return result;
}, [groups]);
const refetch = useCallback(() => {
void query.refetch();
}, [query.refetch]);
return {
isEnabled: externalObjectsFeature.isEnabled,
groups,
markdownReferences,
isLoading: enabled && query.isLoading,
isError: query.isError,
refetch,
};
}
export function useIssueExternalObjectSummary(issueId: string | null | undefined): {
summary: ExternalObjectSummary | null;
isLoading: boolean;
} {
const externalObjectsFeature = useExternalObjectsFeature();
const enabled = externalObjectsFeature.isEnabled && Boolean(issueId);
const query = useQuery({
queryKey: queryKeys.externalObjects.issueSummary(issueId ?? "__none__"),
queryFn: () => externalObjectsApi.getIssueSummary(issueId!),
enabled,
staleTime: 60_000,
});
return {
summary: query.data ?? null,
isLoading: enabled && query.isLoading,
};
}
export function useIssueExternalObjectSummaries(
companyId: string | null | undefined,
issueIds: readonly string[],
): {
summaries: Map<string, ExternalObjectSummary>;
isLoading: boolean;
isReady: boolean;
} {
const externalObjectsFeature = useExternalObjectsFeature();
const normalizedIssueIds = useMemo(
() => [...new Set(issueIds.filter((issueId) => issueId.length > 0))].sort(),
[issueIds],
);
const enabled = externalObjectsFeature.isEnabled && Boolean(companyId) && normalizedIssueIds.length > 0;
const query = useQuery({
queryKey: queryKeys.externalObjects.issueSummaries(companyId ?? "__none__", normalizedIssueIds),
queryFn: () => fetchIssueExternalObjectSummariesInBatches(companyId!, normalizedIssueIds),
enabled,
staleTime: 60_000,
});
const summaries = useMemo(
() => new Map(Object.entries(query.data?.summaries ?? {})),
[query.data?.summaries],
);
return {
summaries,
isLoading: enabled && query.isLoading,
isReady: externalObjectsFeature.isLoaded && (!enabled || query.isSuccess),
};
}
export function useProjectExternalObjectSummary(projectId: string | null | undefined): {
summary: ExternalObjectSummary | null;
isLoading: boolean;
} {
const externalObjectsFeature = useExternalObjectsFeature();
const enabled = externalObjectsFeature.isEnabled && Boolean(projectId);
const query = useQuery({
queryKey: queryKeys.externalObjects.projectSummary(projectId ?? "__none__"),
queryFn: () => externalObjectsApi.getProjectSummary(projectId!),
enabled,
staleTime: 60_000,
});
return {
summary: query.data ?? null,
isLoading: enabled && query.isLoading,
};
}