mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-16 11:59:26 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - People open tasks from task lists, the Inbox and the sidebar many times each hour > - A task's chat shows its first frame only after about seven separate reads finish > - #20355 added one batched `chat-bootstrap` read that returns all of these reads. #20450 used it only for the sidebar's Recents > - The hosted database is CPU-bound, so the UI must open tasks faster and must also send fewer requests > - This pull request uses the bootstrap for task lists, for open intent on any task link, for cold opens and for a hard load of a task URL. It shares one coordination module between all of these paths > - The benefit is that a task opens from cache more often, a cold open sends one chat request instead of about seven, and the first content paint comes sooner ## Linked Issues or Issue Description Refs #20355 (server read that this pull request consumes). Refs #20450 (Recents preload, now on the shared path). **Is your feature request related to a problem?** A click on a task in a task list waited for about seven separate chat reads. The default task list (Coordinator) had no preload at all. A press or a resting hover on a task link sent the same seven reads, and the page then sent a second set. A hard load of a task URL started its chat reads only after the JavaScript bundle and the company list loaded. Sidebar project task links had no open-intent prefetch. **Describe the solution you'd like** Use the single `chat-bootstrap` read everywhere a task is about to open. Let the page's gating reads share a bootstrap that is in flight instead of sending their own requests. Keep the core issue read separate so the header does not wait for the larger read. **Describe alternatives you've considered** Preload 30 rows per list in three requests (the first prototype). Each task costs the server about fifty queries, and a resting hover warms any other row before the click, so this change preloads 10 rows in one request. ## What Changed - `ui/src/lib/issue-chat-bootstrap.ts`: one module decides when to send a bootstrap and lets page reads share it (`readFromIssueChatBootstrap`). Seeding uses the existing `issueChatBootstrapCache` (replace-style reads are refreshed when newer, merge-style reads are filled only when empty, other companies are ignored). It skips tasks that are loading, loaded in the last 30 seconds, or already fresh in cache, under the id or the identifier. It never aborts a request in flight, so a click that unmounts the list still uses the answer. Each request is bound to the account that sent it; a request sent before the first session read is bound to the first account the session reports, and a sign-out revokes it. A page read shares a bootstrap only while its key holds no data (a refresh after a write always reads the server), and waits at most 5 seconds for it. Every ref of a task (id and identifier) is tracked at request start. Entries expire after 30 seconds. - Task lists preload the chat of their top 10 rows in one request, after the list's own reads settle and the browser is idle (`usePreloadTaskChats`, `when-page-settles.ts`): the Coordinator task list (`TaskList` with `preloadCompanyId`), the streamlined and legacy issues lists and the streamlined Inbox. Recents now uses the same path, so a task in both places loads once. A list that stays open preloads each task at most once in five minutes: live events (#20563's lanes) keep a preloaded task's cache current or mark it stale, and a stale key is read from the server, never from an older bootstrap. All seeded keys are per-task keys on the fast live lane, not the ambient lane. - Open intent (press, click, resting hover or focus) on a task with no chat history in cache warms it with one bootstrap request instead of six separate reads (`prefetchIssueDetailForNavigation`). A task seen before still refreshes its cached reads one by one. - A cold open starts one bootstrap. The gating reads (recent comments, activity, runs, input facts, interactions, attachments, live runs, active run) wait for it instead of sending their own requests. The core issue read stays independent. - A hard load of a task URL starts `GET /api/issues/:ref/chat-bootstrap` from the inline preload script, in parallel with the JavaScript bundle. The page adopts that answer. - Sidebar project task links (Coordinator sidebar) get the same hover, press and focus warming as Recents and `IssueLinkQuicklook`. - Tests: unit tests for the coordination module, Recents, `TaskList` preload, sidebar warming and the cold-intent path. A new layout journey checks that a task the Inbox preloaded reveals once at its latest message without reading its chat again. Journeys that hold one separate read now also hold or fail the bootstrap. The Inbox reveal journey held a read that the chat no longer sends on open (it was flaky on master too), so it now holds the recent comment window. - The perf harness gains `PAPERCLIP_ISSUE_PERF_LIST_DWELL_MS` and the README documents it. ## Verification Measurement setup: production UI builds of master (`e01fe7f84a` and the earlier base `8d2bf78323`) and of this branch, served by one local server with `PAPERCLIP_UI_DEV_MIDDLEWARE=false` and embedded PostgreSQL. Only `index.html` changed between variants (both asset sets were present), so the server, database and seeded task were the same. Four rounds of the issue-detail harness, with master and branch alternated in each round: 20 pairs per S1 cell, 40 per S2 cell. The host was shared with other agents (load average 16 to 43), so the spread is large. Content paint (the task is readable), median paired delta, branch minus master: | Scenario | Profile | Pairs | Content paint | Branch faster in | API requests before content (master / branch) | |---|---|---:|---:|---:|---:| | S1 preloaded click (dwell 1500 ms) | unthrottled | 20 | -45 ms | 15/20 | 25 / 18 | | S1 preloaded click (dwell 1500 ms) | Fast 4G, 4x CPU | 20 | -23 ms | 13/20 | 26 / 18 | | S1 click, no dwell | unthrottled | 20 | -51 ms | 15/20 | 25 / 18 | | S1 click, no dwell | Fast 4G, 4x CPU | 20 | -21 ms | 12/20 | 26 / 18 | | S2 cold deep link | unthrottled | 40 | -61 ms | 28/40 | 54 / 40 | | S2 cold deep link | Fast 4G, 4x CPU | 40 | -129 ms | 29/40 | 47 / 39 | Medians (p25 to p75), content paint in ms: S1 dwell unthrottled 295 (283-315) → 248 (217-276). S1 dwell Fast 4G 1133 (1114-1226) → 1093 (853-1176). S1 no dwell unthrottled 295 (277-328) → 260 (233-332). S1 no dwell Fast 4G 984 (842-1196) → 923 (816-1110). S2 unthrottled 1002 (677-1235) → 896 (619-1280). S2 Fast 4G 4643 (4155-5061) → 4516 (4298-4894). Header paint is neutral to slightly later in two cells: S1 dwell unthrottled +39 ms (3/20 faster) and S2 Fast 4G +56 ms (17/40). In those cells the header now paints in the same frame as the content. In the first two rounds, the median gap from header to content fell from 105-157 ms to 29-74 ms unthrottled and from 557 ms to 298 ms for S2 on Fast 4G. Other header cells are -27 to +1 ms. The local server answers each read in a few milliseconds, so the network wait that the preload removes is small here. The request count is the stable result: a cold open sends one chat request instead of about seven. List page: the preload request starts after the first task row is in the DOM (64 ms after it unthrottled, 200-280 ms after it on Fast 4G, page clock). Time to the first visible row, 30-task list: unthrottled master 1863 ms vs branch 1563 ms (6 pairs, branch faster in 5). Fast 4G master 3626 ms vs branch 3727 ms (14 pairs, branch faster in 6, mixed signs). The preload starts after the row paints, so it cannot delay it; the Fast 4G difference is noise. One 10-task bootstrap took 78-162 ms on the server. Checks run: - `pnpm exec vitest run` for the bootstrap, cache, prefetch, Recents, sidebar, quicklook, `TaskList`, issues list, Inbox, issue detail and Coordinator tests: 18 files, 646 tests pass. - `pnpm exec tsc -p ui/tsconfig.json --noEmit`: clean, except missing generated Storybook catalog files that this change does not touch. - `pnpm check:token-gates`: clean. - `tests/e2e/task-loading-frame.spec.ts`, `disconnect-resilience.spec.ts` and `connection-reviews.spec.ts` (production build): 21 passed, 1 skipped. - `tests/perf/issue-detail/layout-stability.spec.ts` and `plan-startup.spec.ts` against the production build: all pass. The Inbox reveal journey and the new preloaded journey passed 4 of 4 repeats each. - `pnpm verify` asks for broader CI coverage because an e2e spec changed. ## Risks - More server work for list views: one bootstrap of up to 10 tasks per list visit (about fifty queries per task), skipped for tasks already loaded in the last 30 seconds. It runs only after the page's own reads settle and the browser is idle, and only while the tab is visible. - A resting hover on a never-opened task now sends one bootstrap instead of about six reads. The server work per task is about the same; the request count is lower. - If a bootstrap fails, each page read falls back to its own request. The fallback is slower than before for that one open (the reads start when the page mounts, not at the press). - The header can paint a few tens of milliseconds later in the same frame as the content when everything is cached. - An answer that arrives after a sign-out or account change is dropped. An answer for another company is ignored. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use (shell, file edits, Playwright runs). ## 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 - [ ] 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
349 lines
15 KiB
TypeScript
349 lines
15 KiB
TypeScript
import type { QueryClient, QueryKey } from "@tanstack/react-query";
|
|
import { ISSUE_CHAT_BOOTSTRAP_MAX_IDS } from "@paperclipai/shared";
|
|
import { api } from "@/api/client";
|
|
import { issuesApi } from "@/api/issues";
|
|
import { EARLY_CHAT_BOOTSTRAP_GLOBAL } from "@/lib/issue-detail-preload-events";
|
|
import { queryKeys } from "@/lib/queryKeys";
|
|
import {
|
|
hasFreshIssueChatFirstFrame,
|
|
ISSUE_CHAT_BOOTSTRAP_FRESH_MS,
|
|
seedIssueChatBootstrap,
|
|
type IssueChatBootstrapEntry,
|
|
} from "@/lib/issueChatBootstrapCache";
|
|
|
|
// A task opens instantly when everything its chat reads before the first frame
|
|
// is already cached. One bootstrap request returns those reads for up to ten
|
|
// tasks, in the shapes of the per-task endpoints, and `issueChatBootstrapCache`
|
|
// writes them under the exact keys the task page reads. This module decides
|
|
// when to send that request and lets the page's reads share it:
|
|
//
|
|
// - Task lists and the sidebar's Recents preload their top rows in the background.
|
|
// - A cold open (direct link, or a task that was not preloaded) starts one
|
|
// bootstrap for that task, and the page's gating reads wait for it instead of
|
|
// sending about ten requests of their own.
|
|
// - A hard load of a task URL adopts the fetch the HTML started (see
|
|
// `vite-issue-detail-preload`).
|
|
|
|
export type ChatBootstrapItem = IssueChatBootstrapEntry;
|
|
|
|
/**
|
|
* How many top rows of a task list are preloaded: one request. Each task
|
|
* costs the server about fifty queries, and a resting hover warms any other
|
|
* row before it is clicked.
|
|
*/
|
|
export const CHAT_BOOTSTRAP_PRELOAD_LIMIT = ISSUE_CHAT_BOOTSTRAP_MAX_IDS;
|
|
/** How long after it arrives a bootstrap answer may stand in for a page read. */
|
|
const SHARE_WINDOW_MS = 2_000;
|
|
/**
|
|
* How long a page read waits for a loading bootstrap before it reads the
|
|
* server itself. A stalled bootstrap must not keep the chat (or its Retry)
|
|
* from loading.
|
|
*/
|
|
export const CHAT_BOOTSTRAP_SHARE_DEADLINE_MS = 5_000;
|
|
|
|
/**
|
|
* The account a request was sent for: a user id, `null` when the session has
|
|
* no user, or `undefined` while this document has not read its session yet.
|
|
* `revoked` marks a request whose answer must never be used.
|
|
*/
|
|
type Owner = { account: string | null | undefined; revoked: boolean };
|
|
|
|
/** One task's part of a bootstrap request. It is never used by another account. */
|
|
type Entry = { promise: Promise<ChatBootstrapItem | undefined>; owner: Owner; startedAt: number; settledAt?: number };
|
|
const entries = new Map<string, Entry>();
|
|
|
|
function refKey(ref: string) {
|
|
return /^[0-9a-f-]{36}$/i.test(ref) ? ref.toLowerCase() : ref.toUpperCase();
|
|
}
|
|
|
|
/** The session's user id, `null` without a user, `undefined` before the first session read. */
|
|
function sessionAccount(queryClient: QueryClient): string | null | undefined {
|
|
const session = queryClient.getQueryState<{ user?: { id?: unknown } | null } | null>(queryKeys.auth.session)?.data;
|
|
if (session === undefined) return undefined;
|
|
return typeof session?.user?.id === "string" ? session.user.id : null;
|
|
}
|
|
|
|
/**
|
|
* Whether `owner`'s answer may be used now. A request sent before the first
|
|
* session read (a hard load's early fetch) can only have used this
|
|
* document's original sign-in, so it belongs to the first account the
|
|
* session reports; a session without a user revokes it.
|
|
*/
|
|
function ownerMatches(owner: Owner, current: string | null | undefined) {
|
|
if (owner.revoked) return false;
|
|
if (owner.account !== undefined) return owner.account === current;
|
|
if (current === undefined) return true;
|
|
if (current === null) {
|
|
owner.revoked = true;
|
|
return false;
|
|
}
|
|
owner.account = current;
|
|
return true;
|
|
}
|
|
|
|
/** Owners sent before the first session read, bound as soon as the session is known. */
|
|
const pendingOwners = new Set<Owner>();
|
|
let stopWatchingSession: (() => void) | null = null;
|
|
|
|
function newOwner(queryClient: QueryClient): Owner {
|
|
const owner: Owner = { account: sessionAccount(queryClient), revoked: false };
|
|
if (owner.account !== undefined) return owner;
|
|
pendingOwners.add(owner);
|
|
stopWatchingSession ??= queryClient.getQueryCache().subscribe(() => {
|
|
const current = sessionAccount(queryClient);
|
|
if (current === undefined) return;
|
|
// Bind (or, for a session without a user, revoke) before anything else
|
|
// can sign in, so a later account never inherits these answers.
|
|
for (const pending of pendingOwners) ownerMatches(pending, current);
|
|
pendingOwners.clear();
|
|
stopWatchingSession?.();
|
|
stopWatchingSession = null;
|
|
});
|
|
return owner;
|
|
}
|
|
|
|
function isExpired(entry: Entry, now: number) {
|
|
return entry.settledAt !== undefined && now - entry.settledAt >= ISSUE_CHAT_BOOTSTRAP_FRESH_MS;
|
|
}
|
|
|
|
/** This account's entry for `ref` that is loading or loaded recently. */
|
|
function currentEntry(queryClient: QueryClient, ref: string, now = Date.now()) {
|
|
const entry = entries.get(refKey(ref));
|
|
if (!entry || isExpired(entry, now)) return undefined;
|
|
return ownerMatches(entry.owner, sessionAccount(queryClient)) ? entry : undefined;
|
|
}
|
|
|
|
/** Remove `entry` from every ref that still points at it. */
|
|
function forget(entry: Entry) {
|
|
for (const [key, value] of entries) if (value === entry) entries.delete(key);
|
|
}
|
|
|
|
/**
|
|
* Track one task's part of a request under every ref it is known by: the
|
|
* refs the caller gave at once, and the task's id and identifier when the
|
|
* answer arrives. A list (identifiers) and Recents (ids) then never load the
|
|
* same task twice, even while a request is in flight. Expired entries are
|
|
* dropped, so answers do not stay in memory.
|
|
*/
|
|
function track(queryClient: QueryClient, refs: readonly string[], owner: Owner, startedAt: number, promise: Promise<ChatBootstrapItem | undefined>) {
|
|
const now = Date.now();
|
|
for (const [key, entry] of entries) if (isExpired(entry, now)) entries.delete(key);
|
|
const entry: Entry = { startedAt, owner, promise };
|
|
for (const ref of refs) entries.set(refKey(ref), entry);
|
|
void promise.then((item) => {
|
|
entry.settledAt = Date.now();
|
|
if (!item) {
|
|
// A failed or empty answer must not suppress later reads or preloads.
|
|
forget(entry);
|
|
return;
|
|
}
|
|
for (const alias of [item.issue.id, item.issue.identifier]) {
|
|
if (alias && !currentEntry(queryClient, alias)) entries.set(refKey(alias), entry);
|
|
}
|
|
});
|
|
}
|
|
|
|
/** One task to request: the ref sent to the server, plus every ref it is known by. */
|
|
type Wanted = { ref: string; refs: string[] };
|
|
|
|
function startChunk(queryClient: QueryClient, companyId: string, wanted: readonly Wanted[]) {
|
|
const refs = wanted.map((target) => target.ref);
|
|
const requestedAt = Date.now();
|
|
const owner = newOwner(queryClient);
|
|
// A synchronous throw (bad input) is only a missed preload as well.
|
|
const request = Promise.resolve().then(() => issuesApi.chatBootstrap(companyId, refs)).then((response) => {
|
|
const byRef = new Map<string, ChatBootstrapItem>();
|
|
// Never seed or share an answer read with the previous account's cookie.
|
|
if (!ownerMatches(owner, sessionAccount(queryClient))) return byRef;
|
|
for (const item of response.items) {
|
|
if (!seedIssueChatBootstrap(queryClient, item, { companyId, requestedAt })) continue;
|
|
byRef.set(refKey(item.ref), item);
|
|
}
|
|
return byRef;
|
|
});
|
|
for (const target of wanted) {
|
|
track(queryClient, target.refs, owner, requestedAt, request.then((byRef) => byRef.get(refKey(target.ref)), () => undefined));
|
|
}
|
|
return request.then(() => undefined, () => undefined);
|
|
}
|
|
|
|
/**
|
|
* How long a background preload (a task list) skips a task it already
|
|
* preloaded. A list that stays open re-renders as live events reorder it;
|
|
* events keep a preloaded task's cache current or mark it stale, so the list
|
|
* does not need to preload the same task again every 30 seconds.
|
|
*/
|
|
export const CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS = 5 * 60_000;
|
|
const backgroundPreloadedAt = new Map<string, number>();
|
|
|
|
function preloadedInBackgroundRecently(refs: readonly string[], now: number) {
|
|
return refs.some((ref) => {
|
|
const at = backgroundPreloadedAt.get(refKey(ref));
|
|
return at !== undefined && now - at < CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS;
|
|
});
|
|
}
|
|
|
|
function rememberBackgroundPreload(refs: readonly string[], now: number) {
|
|
for (const [key, at] of backgroundPreloadedAt) if (now - at >= CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS) backgroundPreloadedAt.delete(key);
|
|
for (const ref of refs) backgroundPreloadedAt.set(refKey(ref), now);
|
|
}
|
|
|
|
/** A task to preload: a ref, or its id with the identifier it may also be cached under. */
|
|
export type ChatBootstrapTarget = string | { id: string; identifier: string | null };
|
|
|
|
/**
|
|
* Load the bootstrap for these tasks in requests of at most ten, one request
|
|
* at a time, and seed the cache as each one arrives. Tasks loading, loaded
|
|
* within `ISSUE_CHAT_BOOTSTRAP_FRESH_MS`, or whose first frame is already cached
|
|
* and fresh are skipped.
|
|
*
|
|
* `signal` stops later requests, never one in flight: a click that unmounts
|
|
* the list must still be able to use the answer for the task it opens.
|
|
* `background` (a list's preload) also skips tasks it preloaded within
|
|
* `CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS`.
|
|
*/
|
|
export async function loadIssueChatBootstraps(
|
|
queryClient: QueryClient,
|
|
companyId: string,
|
|
targets: readonly ChatBootstrapTarget[],
|
|
options: { signal?: AbortSignal; background?: boolean } = {},
|
|
) {
|
|
const now = Date.now();
|
|
// Keyed by the normalized ref; the request keeps the caller's spelling.
|
|
const wanted = new Map<string, Wanted>();
|
|
for (const target of targets) {
|
|
const refs = typeof target === "string" ? [target] : [target.id, target.identifier].filter((ref): ref is string => !!ref);
|
|
if (!refs.length || wanted.has(refKey(refs[0]!))) continue;
|
|
if (refs.some((ref) => currentEntry(queryClient, ref, now))) continue;
|
|
if (hasFreshIssueChatFirstFrame(queryClient, refs, now)) continue;
|
|
if (options.background && preloadedInBackgroundRecently(refs, now)) continue;
|
|
wanted.set(refKey(refs[0]!), { ref: refs[0]!, refs });
|
|
}
|
|
const ordered = [...wanted.values()];
|
|
for (let start = 0; start < ordered.length; start += ISSUE_CHAT_BOOTSTRAP_MAX_IDS) {
|
|
if (options.signal?.aborted) return;
|
|
const chunk = ordered.slice(start, start + ISSUE_CHAT_BOOTSTRAP_MAX_IDS);
|
|
if (options.background) rememberBackgroundPreload(chunk.flatMap((target) => target.refs), Date.now());
|
|
await startChunk(queryClient, companyId, chunk);
|
|
}
|
|
}
|
|
|
|
/** Set by the inline script; `startedAt` is the request's `Date.now()`. */
|
|
type EarlyChatBootstrap = { ref: string; startedAt?: number; promise: Promise<ChatBootstrapItem | null> };
|
|
|
|
/**
|
|
* Adopt the fetch the page's inline script started on a hard load of this task
|
|
* (see `vite-issue-detail-preload`), so the first answer needs no new request.
|
|
*/
|
|
function adoptEarlyChatBootstrap(queryClient: QueryClient, companyId: string | null | undefined, ref: string) {
|
|
if (typeof window === "undefined") return false;
|
|
const holder = window as unknown as Record<string, EarlyChatBootstrap | undefined>;
|
|
const early = holder[EARLY_CHAT_BOOTSTRAP_GLOBAL];
|
|
if (!early || refKey(early.ref) !== refKey(ref)) return false;
|
|
delete holder[EARLY_CHAT_BOOTSTRAP_GLOBAL];
|
|
// Staleness counts from when the server could last have read the task.
|
|
const startedAt = typeof early.startedAt === "number" ? early.startedAt : Date.now();
|
|
// A page that took this long to start reads the task itself.
|
|
if (Date.now() - startedAt >= ISSUE_CHAT_BOOTSTRAP_FRESH_MS) return false;
|
|
const owner = newOwner(queryClient);
|
|
track(queryClient, [ref], owner, startedAt, early.promise.then((item) => {
|
|
// The answer belongs to the account that loaded the page.
|
|
if (!item?.issue || !ownerMatches(owner, sessionAccount(queryClient))) return undefined;
|
|
// The route's task is the one the page opens; a known company must match it.
|
|
const ownerCompanyId = companyId ?? item.issue.companyId;
|
|
return seedIssueChatBootstrap(queryClient, item, { companyId: ownerCompanyId, requestedAt: startedAt }) ? item : undefined;
|
|
}, () => undefined));
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* The single-task route, for a task whose company the caller does not know
|
|
* (a link's open intent). The task's own company owns the answer.
|
|
*/
|
|
function startSingle(queryClient: QueryClient, ref: string) {
|
|
const requestedAt = Date.now();
|
|
const owner = newOwner(queryClient);
|
|
const path = `/issues/${encodeURIComponent(ref)}/chat-bootstrap`;
|
|
track(queryClient, [ref], owner, requestedAt, Promise.resolve().then(() => api.get<ChatBootstrapItem>(path, { cache: "no-store" })).then((item) => {
|
|
if (!item?.issue || !ownerMatches(owner, sessionAccount(queryClient))) return undefined;
|
|
return seedIssueChatBootstrap(queryClient, item, { companyId: item.issue.companyId, requestedAt }) ? item : undefined;
|
|
}, () => undefined));
|
|
}
|
|
|
|
/**
|
|
* Start a single-task bootstrap for a cold open or an open intent, unless one
|
|
* is loading, loaded recently, or the task's first frame is cached and fresh.
|
|
* Without a company it uses the single-task route. Resolves when the task's
|
|
* bootstrap (new or already loading) settles; it never rejects.
|
|
*/
|
|
export function startIssueChatBootstrap(
|
|
queryClient: QueryClient,
|
|
companyId: string | null | undefined,
|
|
ref: string | null | undefined,
|
|
): Promise<unknown> {
|
|
if (!ref) return Promise.resolve();
|
|
const settled = () => currentEntry(queryClient, ref)?.promise ?? Promise.resolve();
|
|
if (currentEntry(queryClient, ref)) return settled();
|
|
if (adoptEarlyChatBootstrap(queryClient, companyId, ref)) return settled();
|
|
if (companyId) {
|
|
void loadIssueChatBootstraps(queryClient, companyId, [ref]);
|
|
return settled();
|
|
}
|
|
if (hasFreshIssueChatFirstFrame(queryClient, [ref])) return Promise.resolve();
|
|
startSingle(queryClient, ref);
|
|
return settled();
|
|
}
|
|
|
|
/**
|
|
* For a task page's first read of `key`: when this account's bootstrap for
|
|
* the task is loading or just arrived, use its part of the answer instead of
|
|
* sending another request. A key that already holds data is being refreshed
|
|
* (after a write, an invalidation or a poll), so it always asks the server.
|
|
* Falls back to the normal read when there is no bootstrap, or it lacks the task.
|
|
*/
|
|
export async function readFromIssueChatBootstrap<T>(
|
|
queryClient: QueryClient,
|
|
key: QueryKey,
|
|
ref: string,
|
|
pick: (item: ChatBootstrapItem) => T | undefined,
|
|
read: () => Promise<T>,
|
|
): Promise<T> {
|
|
if (queryClient.getQueryData(key) !== undefined) return read();
|
|
const entry = currentEntry(queryClient, ref);
|
|
// Only share an answer that is still loading or a moment old.
|
|
if (entry && (entry.settledAt === undefined || Date.now() - entry.settledAt < SHARE_WINDOW_MS)) {
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
const stalled = new Promise<"stalled">((resolve) => { timer = setTimeout(() => resolve("stalled"), CHAT_BOOTSTRAP_SHARE_DEADLINE_MS); });
|
|
const answer = await Promise.race([entry.promise, stalled]);
|
|
clearTimeout(timer);
|
|
if (answer === "stalled") {
|
|
// Later reads (and Retry) go to the server instead of the stuck request.
|
|
forget(entry);
|
|
return read();
|
|
}
|
|
const item = answer;
|
|
const value = item ? pick(item) : undefined;
|
|
if (value !== undefined) return value;
|
|
}
|
|
return read();
|
|
}
|
|
|
|
/** Whether two sorted id lists are the same set. */
|
|
export function sameIds(a: readonly string[], b: readonly string[]) {
|
|
return a.length === b.length && a.every((id, index) => id === b[index]);
|
|
}
|
|
|
|
/** Test seam. */
|
|
export function resetIssueChatBootstrapsForTest() {
|
|
entries.clear();
|
|
backgroundPreloadedAt.clear();
|
|
pendingOwners.clear();
|
|
stopWatchingSession?.();
|
|
stopWatchingSession = null;
|
|
}
|
|
|
|
/** Test seam: how many refs hold an entry. */
|
|
export function issueChatBootstrapEntryCountForTest() {
|
|
return entries.size;
|
|
}
|