WeKnora/internal/handler/session
ochan.kwon 894cbec48a feat(agent): report and attribute per-turn LLM token usage
Accumulate token usage across every LLM round of an agent turn and
expose the aggregate everywhere a turn is observable:

- AgentState.TurnUsage accumulates each round's reported usage,
  including the final-answer synthesis call; TokenUsage.Accumulate
  keeps the cache counters as prompt subsets and recomputes the
  combined cache status (unsupported survives only while every call
  was itself unsupported).
- The agent completion event carries the aggregate, and the complete
  stream event exposes it both as data.usage and as a typed
  StreamEvent.Usage that buildStreamResponse promotes into the
  previously-unused StreamResponse.Usage wire field.
- The assistant message persists it as a nullable usage column on
  both migration targets (postgres 000085 jsonb, sqlite 000012 text),
  for web and IM channels alike, so history reads still carry the
  turn's cost after the live stream is gone.
- "[LLM Usage]" log lines gain ", session_id=..., principal=type:id"
  when the context carries them; calls outside sessions stay
  byte-identical.

Usage from failed retry attempts is not accumulated: the per-call
usage log remains the ground truth, while the turn aggregate serves
as the operational signal.
2026-08-24 10:36:19 +08:00
..
agent_stream_handler.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00
artifact_download.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
artifact_download_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
attachment_processor.go feat(docparser): 引入 anydoc 引擎,在 Go 进程内解析 office 文档 2026-08-16 11:37:27 +08:00
continue_stream_resource_urls_test.go test(api): cover resource_urls=public across the rewriter, handlers and SSE wire 2026-08-05 14:42:51 +08:00
handler.go feat(memory): add cross-session long-term memory 2026-08-13 18:00:02 +08:00
helpers.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00
helpers_test.go refactor(handler): extract shared searchResultFromMap 2026-08-10 19:34:02 +08:00
image_upload.go feat(storage): add multi-instance storage backends per workspace 2026-07-15 14:54:39 +08:00
qa.go fix(chat): 持久化快速问答的检索步骤,刷新后不再丢失时间轴 2026-08-13 21:34:21 +08:00
qa_attachment_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
quick_answer_timeline.go fix(chat): 持久化快速问答的检索步骤,刷新后不再丢失时间轴 2026-08-13 21:34:21 +08:00
quick_answer_timeline_test.go fix(chat): 持久化快速问答的检索步骤,刷新后不再丢失时间轴 2026-08-13 21:34:21 +08:00
resolve_agent_test.go fix: harden shared agent source selector validation 2026-07-30 12:28:23 +08:00
resource_urls.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
resource_urls_test.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
search_knowledge_resource_urls_test.go fix(api): address resource_urls review follow-ups 2026-08-05 14:42:51 +08:00
stream.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
stream_usage_test.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00
temporary_attachment_persist_test.go feat(attachments): implement normalization and limits for temporary attachment IDs 2026-07-16 17:41:27 +08:00
temporary_document.go fix: harden shared agent source selector validation 2026-07-30 12:28:23 +08:00
title.go feat(session): implement strict owner scope for API-key sessions 2026-07-20 19:31:02 +08:00
types.go fix: harden shared agent source permissions 2026-07-30 12:28:23 +08:00
wiki_fixer_scope.go fix(session): scope wiki fixer to shared KB tenant 2026-05-21 11:19:19 +08:00
wiki_fixer_scope_test.go fix(session): scope wiki fixer to shared KB tenant 2026-05-21 11:19:19 +08:00