Skip to content

[feat] agent: record token usage, compact project views, and precheck - #341

Open
BeatSeat wants to merge 2 commits into
ThinkFlowLab:mainfrom
BeatSeat:feat/agent-token-observability-and-project-summary
Open

BeatSeat wants to merge 2 commits into
ThinkFlowLab:mainfrom
BeatSeat:feat/agent-token-observability-and-project-summary

Conversation

@BeatSeat

Copy link
Copy Markdown

Summary

  • Record provider-neutral token and KV-cache metrics (promptTokens, completionTokens, totalTokens, cachedPromptTokens, cacheMissPromptTokens, reasoningTokens, and cacheHitRate) in ModelGateway across SSE streams and bounded 2xx JSON responses, emitting an idempotent turn.usage summary on turn close without modifying stream framing or response bytes.
  • Extend project.get, read_project, and opendirector project show with view="summary" and itemIds scoping to bound multi-node Agent context bytes while preserving authoritative object identities and revisions.
  • Add side-effect-free generation.precheck across HTTP /queries, Agent query, and opendirector generation precheck, sharing backend generation validation, lock and queue checks, duration/byte estimates, confirmation hints, and Accept-Language error localization.
  • Regenerate src/contracts/creation.schema.json and src/contracts/generated.ts, and document the new CLI flags in both Chinese and English in docs/guides/cli.md.

Linked issue

  • Fixes #N or Refs #N is included, or no issue applies

Fixes #337
Refs #188, #261

Labels

Suggested taxonomy labels (external fork contributor; automated classifier / maintainer can apply): type:feat, area:agent, area:canvas, area:docs.

  • Labels applied

Test plan

  • Local:
    • python scripts/generate_contracts.py --check (verified)
    • lint-imports (134 files analyzed, 4 contracts kept, 0 broken)
    • pytest tests/test_model_gateway.py tests/test_agent_observability_and_queries.py tests/test_creation_contracts.py tests/test_agent_metadata.py tests/test_agent_gateway.py tests/test_cli.py -q (passed)
    • npm run check:i18n (3014 keys, 0 problems)
    • Static AST, JSON, LF line-ending, EOF newline, and trailing-whitespace checks across the full PR diff against origin/main
  • Left to CI:
    • Full Python suite + bundled OpenCode regression (OPENDIRECTOR_REAL_CLI=1), npm run build (node_modules TypeScript compiler not installed in the local container), and browser lanes.

Evidence that it works

  • Media attached, or N/A because: Backend Agent gateway, read-only query contract, and CLI additions with no web UI layout changes; verified end-to-end through FastAPI TestClient, ToolGateway, ModelGateway loopback SSE/JSON streams, and CliRunner JSON receipts.

Provider verification

  • Real-provider calls are described honestly, or none were made

No live commercial or GPU provider calls were made. SSE and JSON usage normalization (prompt_tokens_details.cached_tokens, prompt_cache_hit_tokens, cache_read_input_tokens, and completion_tokens_details.reasoning_tokens) and generation.precheck validation were verified using deterministic local HTTP/SSE transports and real local project storage.

Bilingual UI

  • Both locales are updated, or no user-facing copy changed

Accept-Language localization (zh-CN and en-US) is wired for generation.precheck issues on /queries, docs/guides/cli.md is updated in both Chinese and English, and npm run check:i18n passed with zero issues.

## summary

- Record provider-neutral token and KV-cache metrics (`promptTokens`,
  `completionTokens`, `totalTokens`, `cachedPromptTokens`,
  `cacheMissPromptTokens`, `reasoningTokens`, and `cacheHitRate`) in
  `ModelGateway` for SSE and bounded 2xx JSON responses, emitting an
  idempotent `turn.usage` summary on turn close without altering stream
  framing.
- Extend `project.get`, `read_project`, and `opendirector project show`
  with `view="summary"` and `itemIds` scoping to cut multi-node context
  bytes while preserving authoritative object identities and revisions.
- Add side-effect-free `generation.precheck` across HTTP `/queries`,
  Agent `query`, and `opendirector generation precheck`, sharing backend
  generation validation, lock and queue checks, duration/byte estimates,
  confirmation hints, and `Accept-Language` error localization.
- Document the CLI options in Chinese and English and cover transport,
  HTTP, Agent gateway, and CLI equivalence in automated tests.

## validation

- `python scripts/generate_contracts.py --check`
- `lint-imports`
- `pytest tests/test_model_gateway.py tests/test_agent_observability_and_queries.py tests/test_agent_metadata.py tests/test_agent_gateway.py tests/test_cli.py -q`
- `npm run check:i18n`

Fixes: ThinkFlowLab#337
Refs: ThinkFlowLab#188, ThinkFlowLab#261
@github-actions github-actions Bot added area:agent Agent runtime, assistant chat and tools area:canvas Canvas, nodes and direct manipulation area:docs Documentation / 文档 type:feat New capability or behaviour labels Nov 15, 2026
## summary

- Merge split SSE usage chunks and parse `input_tokens_details`,
  `output_tokens_details`, `cache_read_input_tokens`, and
  `cache_creation_input_tokens` in `ModelGateway` so multi-chunk streams
  and cache read/write dialects preserve prompt and cache metrics.
- Include sanitized `models` in `project.get` `view="summary"` to match
  `read_project` and the `ThinkFlowLab#337` compact summary contract across HTTP and
  CLI queries.
- Align `generation.precheck` with `CreationGeneration.prepare` for
  tool-only protocols, MiniMax model allowlists, image/video output
  sizes and resolutions, continuation aspect ratios, default durations,
  and reference-music score helpers.
- Add regression tests for split SSE usage chunks, summary `models`, and
  `generation.precheck` vs `CreationGeneration.prepare` rejection parity.

## validation

- `.venv/bin/python3 scripts/generate_contracts.py --check`
- `PYTHONPATH=packages/core/src:apps/api/src .venv/bin/lint-imports`
- `PYTHONPATH=packages/core/src:apps/api/src .venv/bin/python3 -m pytest tests/test_model_gateway.py tests/test_agent_observability_and_queries.py tests/test_creation_contracts.py tests/test_agent_metadata.py tests/test_agent_gateway.py tests/test_cli.py -q`
- `npm run check:i18n`

Refs: ThinkFlowLab#337
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:canvas Canvas, nodes and direct manipulation area:docs Documentation / 文档 type:feat New capability or behaviour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feat] agent: 支持模型网关 Token/KV 缓存观测、紧凑工程视图与零副作用生成预检

1 participant