deerflow2/backend/app/gateway/routers
Ryker_Feng 167ef4512f
feat(memory): add memory.token_counting config to avoid tiktoken network dependency (#3429) (#3465)
* feat(memory): add memory.token_counting config to avoid tiktoken network dependency (#3429)

Add a `memory.token_counting` option (`tiktoken` | `char`) so deployments in
network-restricted environments can opt out of tiktoken entirely. In `char`
mode the memory-injection budget uses a network-free character-based estimate
and never triggers the BPE download from openaipublic.blob.core.windows.net,
which could otherwise block for tens of minutes (see #3402).

Also harden the default `tiktoken` path:
- cache an in-flight LOADING sentinel so concurrent callers fall back
  immediately instead of spawning more blocking get_encoding threads when the
  first load is still running (e.g. under the 5s startup warm-up timeout);
- cache failures with a timestamp and retry after a cooldown so a transient
  network outage self-heals back to accurate counting without a restart;
- skip startup warm-up entirely in char mode.

The new config is surfaced via the memory config API and config.example.yaml
(config_version bumped). Default remains `tiktoken`, so existing deployments
are unaffected.

* fix(memory): use CJK-aware char token estimate and address review feedback

- Replace the flat len(text)//4 fallback with a CJK-aware estimate so
  Chinese/Japanese/Korean memory content does not over-fill the injection budget
- Document the internal tiktoken retry cooldown and char-mode escape hatch
- Sync CLAUDE.md / config.example.yaml / MEMORY_IMPROVEMENTS.md wording
- Fix MemoryConfigResponse mocks/assertions and add CJK estimate tests
2026-06-10 23:26:15 +08:00
..
__init__.py feat(gateway): implement LangGraph Platform API in Gateway, replace langgraph-cli (#1403) 2026-03-30 16:02:23 +08:00
agents.py fix(agents): offload blocking filesystem IO in the custom-agent router off the event loop (#3457) 2026-06-09 22:24:53 +08:00
artifacts.py fix(gateway): cap skill artifact preview size (#2963) 2026-05-15 22:15:58 +08:00
assistants_compat.py feat(gateway): implement LangGraph Platform API in Gateway, replace langgraph-cli (#1403) 2026-03-30 16:02:23 +08:00
auth.py fix: align auth-disabled mode and mock history loading (#3471) 2026-06-10 16:11:00 +08:00
channels.py refactor: split backend into harness (deerflow.*) and app (app.*) (#1131) 2026-03-14 22:55:52 +08:00
feedback.py feat(persistence):Unified persistence layer with event store, feedback, and rebase cleanup (#2134) 2026-04-26 11:09:55 +08:00
mcp.py fix(security): harden MCP config endpoint (#3425) 2026-06-08 12:21:02 +08:00
memory.py feat(memory): add memory.token_counting config to avoid tiktoken network dependency (#3429) (#3465) 2026-06-10 23:26:15 +08:00
models.py refactor: thread release config through lead path (#2612) 2026-04-28 14:53:18 +08:00
runs.py fix: load paginated run history messages (#3305) 2026-06-01 15:50:39 +08:00
skills.py refactor(skills): Unified skill storage capability (#2613) 2026-05-01 13:23:26 +08:00
suggestions.py fix(suggestions): strip inline <think> reasoning before parsing follow-up questions (#3435) 2026-06-08 15:48:00 +08:00
thread_runs.py fix: load paginated run history messages (#3305) 2026-06-01 15:50:39 +08:00
threads.py fix(threads): assign new checkpoint ID in update_thread_state (#2391) 2026-06-08 23:12:25 +08:00
uploads.py fix upload file size contract (#3408) 2026-06-06 15:12:17 +08:00