Skip to content

[perf] assistant: insert chat references without the project-wide catalog - #594

Merged
princepride merged 2 commits into
ThinkFlowLab:mainfrom
princepride:perf/assistant-reference-picker
Nov 29, 2026
Merged

princepride merged 2 commits into
ThinkFlowLab:mainfrom
princepride:perf/assistant-reference-picker

Conversation

@princepride

@princepride princepride commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Referencing canvas media in the assistant chat (AI 对话) waited on the project-wide reference catalog twice, and the second wait came with extra work:

  • Typing @ showed only "loading" until /reference-catalog arrived, even though the chat's options are the visible canvas nodes it already has. The generation composer's picker already passes loading={false} for this reason.

  • Choosing a node:

    1. re-read the canvas;
    2. read the whole catalog again (each asset's file checked, plus a scan of the commit history);
    3. downloaded each referenced file;
    4. SHA-256 hashed it in the browser.

    All of this ran before the reference chip appeared, and its cost grows with project history, asset count and file size.

This PR:

  • The chat picker no longer waits on the catalog. Opening it still prefetches the catalog in the background, for refreshing draft references and pasting.

  • GET /reference-catalog?nodeId=… (up to 64) returns only the chosen nodes' media. It reads the head snapshot (no history scan) and checks only those files. For a node's own media the rows and availability match the full catalog's rows for that node; the full catalog without nodeId is unchanged. The browser sends this read in the foreground rather than queueing it behind other background project reads.

  • A pick uses that read only. It no longer re-reads the canvas, downloads the files, or hashes them in the browser:

    • the canvas is saved (onPrepareReferences) before the pick, so the server resolves the nodes' current media itself;
    • the server checks each file against its recorded fingerprint;
    • /content also refuses bytes that don't match.

    The now-unused assistant.error.referenceChangedBatch message is removed in both locales.

The server accepts at most 64 nodeId values per read, so loadNodeReferences splits longer lists into batches of 64. In the chat, a selection of more than 64 nodes is already refused before any read, by pendingReferenceInsertion (unchanged from main), so no current caller sends more.

Paste of a copied reference (onPasteReference) is unchanged.

Linked issue

  • No issue applies. Reported directly as referencing media in the assistant chat loading very slowly.

Labels

  • Labels applied (type:bug, area:agent, area:assets, area:performance)

Test plan

  • Local:
    • npx tsc --noEmit

    • npm test (1033 passed, including the new tests/nodeReferences.test.ts for the batching)

    • npm run check:i18n

    • node scripts/check-browser-manifest.mjs

    • pytest tests/test_creation_history_service.py tests/test_creation_node_versions.py tests/test_creation_api.py (66 passed), including the new test_node_reference_catalog_reads_only_the_chosen_nodes_media, which checks for that node:

      • the same rows as the full catalog;
      • no history read and exactly one file opened;
      • an unknown node returns nothing;
      • a tampered file is reported unavailable.
    • New tests/browser/assistant_chat_references.mjs (extendedFixture and production lanes), run against creation_server.py --assistant-fixture + Vite with Chrome for Testing. It holds the full catalog the whole time and checks that:

      • the @ picker still lists the nodes;
      • a pick sends only the nodeId read, with no /content or /canvas request;
      • a node the server reports unusable is refused with the existing message;
      • the sent message carries the referenced asset ID and fingerprint;
      • selecting 65 canvas nodes shows "每次最多添加 64 份参考素材,整批未插入。", inserts nothing and reads no node references.

      It passes on this branch and times out on main, where the picker waits for the held catalog.

    • tests/browser/reference_picker.mjs, tests/browser/canvas_reference_sources.mjs and tests/browser/assistant_sessions.mjs pass.

  • Left to CI: the rest of the browser suite and the Python suite.

Evidence that it works

  • N/A for media: the chat looks the same. Measured in the browser instead, on isolated --assistant-fixture services, with main and this branch side by side. Each run used a fresh project: 30 distinct ~7 MB videos and 10 images on the canvas, plus 300 text assets for a longer history. Values are from two runs each.
main this PR
Type @ → options shown 206–214 ms 45–48 ms
First node pick → reference chip 964–967 ms 151–152 ms
Second node pick → reference chip 923–931 ms 124 ms
Requests per pick canvas, tool tasks, full catalog (338–517 ms server), file download one nodeId catalog read (~40 ms)

On main the per-pick cost grows with history length, asset count (each catalog read checks every file) and the size of the chosen files (download plus browser hash). With this PR it depends only on the chosen nodes.

Provider verification

  • No provider calls were made; the assistant reply is the fixture.

Bilingual UI

  • No copy added; one unused message removed from both zh-CN and en-US.

🤖 Generated with Claude Code

…alog

Typing @ in the assistant chat showed only "loading" until the whole
reference catalog arrived, although the chat lists the visible canvas nodes
it already has. Choosing a node then re-read the canvas, read the whole
catalog again (every asset's file checked, plus a scan of the commit history)
and downloaded and SHA-256 hashed each referenced file in the browser, all
before the reference appeared.

- The chat picker no longer waits on the catalog, as the generation
  composer's picker already does; opening it still prefetches the catalog
  for draft references in the background.
- reference-catalog accepts nodeId filters. Scoped to the chosen nodes it
  reads only the head snapshot and checks only those nodes' files, with the
  same rows and availability as the full catalog. The browser reads it in the
  foreground instead of behind other background reads.
- A pick uses that read and drops the canvas re-read and the browser
  download and hash: the canvas is saved before the pick, the server checks
  each file against its fingerprint, and media reads refuse changed bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@princepride princepride added area:agent Agent runtime, assistant chat and tools area:assets Assets, library and reference inputs area:performance Performance and responsiveness / 性能与响应速度 type:bug Something is broken labels Nov 29, 2026
Review of this PR found that the node-scoped catalog read accepts at most 64
nodeId values and answers more with a generic validation error. Through the
chat UI a selection is already refused above 64 nodes before any read
(pendingReferenceInsertion, unchanged from main), so no current caller sends
more. loadNodeReferences now splits longer lists into reads of 64 anyway, so
the function is correct for any caller.

The browser test now also selects 65 canvas nodes and checks that the chat
shows its own limit message, inserts nothing and reads no node references; a
unit test covers the batching.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@princepride
princepride merged commit 3710a2c into ThinkFlowLab:main Nov 29, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent runtime, assistant chat and tools area:assets Assets, library and reference inputs area:performance Performance and responsiveness / 性能与响应速度 type:bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant