fix(assistant): stop the chat loading in stages (#1210)

* fix(assistant): stop the chat loading in stages

PR2 of the assistant UI makeover (dev_docs/assistant_redesign_plan.md section 7).
No redesign; this is the "it loads in different stages" complaint, traced to
four separate staging points and one dead link.

Resumed conversations rendered a column of EMPTY bordered cards until the
markdown chunk arrived, then filled in all at once and reflowed the thread. The
chunk was deferred with a null fallback, which is invisible while a reply
streams (nobody reads that fast) but very visible on hydrate, where every
assistant bubble is already text. The chunk is now prefetched as soon as any
chat surface mounts, and until it resolves the raw text renders instead of
nothing, so a bubble is never blank.

Clicking the assistant launcher showed NOTHING until the sheet chunk loaded:
the dynamic import had no loading state at all. It now renders a skeleton in
the same geometry, and the chunk is warmed on idle so the click usually hits an
already-loaded module.

/chat's route skeleton drew a 320px sidebar while ChatSidebar mounts collapsed
as a 48px rail, so every load snapped one to the other. The skeleton now matches
what actually mounts, per breakpoint.

The first turn read agent_profiles twice: once in the route to build the intent's
prompt template, once again in run-turn for the system prompt. The route now
hands its result over. Ranked memory is deliberately NOT shared: the two queries
differ (the route's selects fewer columns and orders without is_pinned, and
run-turn needs ids to stamp last_accessed_at), so reusing it would silently
change both the prompt and memory touch.

Command palette's "Fråga Anna: ..." pointed at /chat?prompt=, but only /chat/new
reads ?prompt=, so the typed question was silently dropped and the user landed
on an empty state.

Verified: 9526 unit tests pass, lint clean and tsc clean on every touched file,
guards pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(assistant): make the prefetches fail-safe and bounded

Review follow-ups on the staged-loading batch.

A rejected markdown import left the cached promise permanently rejected, so
every bubble for the rest of the session stayed on the plain-text fallback and
the rejection went unhandled. The cache is now cleared on failure so a later
surface retries, and the rejection is swallowed.

requestIdleCallback can defer indefinitely on a page that never goes idle; the
2s fallback only applied where the API is missing. The idle request now carries
a 2s timeout, and the warm import cannot produce an unhandled rejection either.

Adds the first-turn test for the profile-summary handover: it asserts the value
read for the prompt template is what reaches the turn, so a regression that
re-introduces the second read (or drops the template) fails.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-26 18:10:47 +02:00
committed by GitHub
co-authored by Claude Opus 5
parent ee8ddb3849
commit f0f3050f54
7 changed files with 183 additions and 14 deletions
+16 -1
View File
@@ -105,6 +105,15 @@ interface RunTurnArgs {
// still persisted for Anthropic context on subsequent turns, but flagged
// hidden=true so /chat/[id] hydration doesn't surface it as a user bubble.
userMessageHidden?: boolean
// Profile summary the caller already loaded for this turn (the invoke route
// reads it to build a first-turn prompt template). Passed through so the same
// read doesn't happen twice per turn.
//
// Ranked memory is deliberately NOT shared: the route's variant selects fewer
// columns and orders without is_pinned, and this one needs ids to stamp
// last_accessed_at. Reusing it there would silently change both the prompt
// and memory touch.
preloadedProfileSummary?: string | null
// Emit events back to the caller. Returns false if the stream was cancelled
// and the loop should stop emitting (best-effort).
emit: (event: StreamEvent) => boolean
@@ -173,8 +182,14 @@ export async function runChatTurn(args: RunTurnArgs): Promise<void> {
} = args
// 1 + 2: load profile + ranked memory + atoms + tools.
//
// On a first turn the caller already read the profile summary to build the
// intent's prompt template, so it hands it over rather than making the same
// round trip again for the system prompt.
const [profile, memory, vatStatus] = await Promise.all([
loadProfileSummary(supabase, companyId),
args.preloadedProfileSummary !== undefined
? Promise.resolve(args.preloadedProfileSummary)
: loadProfileSummary(supabase, companyId),
loadRankedMemory(supabase, companyId, 30),
loadVatStatus(supabase, companyId),
])