@@ -4320,6 +4320,18 @@ func mapSessionRecord(record tables.WebSessionTable) SessionSummary {
43204320 ContextEstimate : contextEstimate ,
43214321 ContextEstimateMode : contextEstimateMode ,
43224322 LastContextCompactionAt : record .LastContextCompactionAt ,
4323+ ContextWindowTokens : func () * int64 {
4324+ if record .SessionContextWindowTokens <= 0 {
4325+ return nil
4326+ }
4327+ return ptr (record .SessionContextWindowTokens )
4328+ }(),
4329+ ContextWindowSource : func () ContextWindowSource {
4330+ if record .SessionContextWindowTokens > 0 {
4331+ return ContextWindowSourceSessionUsage
4332+ }
4333+ return ""
4334+ }(),
43234335 }
43244336}
43254337
@@ -4376,7 +4388,26 @@ func buildLatestTurnUsage(record tables.WebSessionTable) (ContextEstimate, bool)
43764388 return buildRecordedLatestTurnUsage (record )
43774389}
43784390
4391+ func buildLatestTokenCountUsage (record tables.WebSessionTable ) (ContextEstimate , bool ) {
4392+ if record .LatestTokenCountUpdatedAt == nil {
4393+ return ContextEstimate {}, false
4394+ }
4395+ estimate := ContextEstimate {
4396+ InputTokens : maxInt64 (0 , record .LatestTokenCountInputTokens ),
4397+ CachedInputTokens : maxInt64 (0 , record .LatestTokenCountCachedInputTokens ),
4398+ OutputTokens : maxInt64 (0 , record .LatestTokenCountOutputTokens ),
4399+ }
4400+ estimate .UsedTokens = contextEstimateUsedTokens (estimate .InputTokens , estimate .OutputTokens )
4401+ if estimate .UsedTokens == 0 && record .LatestTokenCountTotalTokens > 0 {
4402+ estimate .UsedTokens = record .LatestTokenCountTotalTokens
4403+ }
4404+ return estimate , contextEstimateHasValue (estimate )
4405+ }
4406+
43794407func buildContextEstimate (record tables.WebSessionTable ) (ContextEstimate , ContextEstimateMode ) {
4408+ if latestTokenCount , ok := buildLatestTokenCountUsage (record ); ok {
4409+ return latestTokenCount , ContextEstimateModeLatestTokenCount
4410+ }
43804411 if latestTurnUsage , ok := buildLatestTurnUsage (record ); ok {
43814412 return latestTurnUsage , ContextEstimateModeLatestTurnDelta
43824413 }
@@ -4408,19 +4439,29 @@ func maxInt64(left, right int64) int64 {
44084439
44094440func contextEstimateTotalsUpdate (in , cin , out int64 ) map [string ]any {
44104441 return map [string ]any {
4411- "total_input_tokens" : in ,
4412- "total_cached_input_tokens" : cin ,
4413- "total_output_tokens" : out ,
4414- "updated_at" : time .Now (),
4442+ "total_input_tokens" : in ,
4443+ "total_cached_input_tokens" : cin ,
4444+ "total_output_tokens" : out ,
4445+ "latest_token_count_input_tokens" : 0 ,
4446+ "latest_token_count_cached_input_tokens" : 0 ,
4447+ "latest_token_count_output_tokens" : 0 ,
4448+ "latest_token_count_total_tokens" : 0 ,
4449+ "latest_token_count_updated_at" : nil ,
4450+ "updated_at" : time .Now (),
44154451 }
44164452}
44174453
44184454func contextEstimateIncrementUpdate (in , cin , out int64 ) map [string ]any {
44194455 return map [string ]any {
4420- "total_input_tokens" : gorm .Expr ("total_input_tokens + ?" , in ),
4421- "total_cached_input_tokens" : gorm .Expr ("total_cached_input_tokens + ?" , cin ),
4422- "total_output_tokens" : gorm .Expr ("total_output_tokens + ?" , out ),
4423- "updated_at" : time .Now (),
4456+ "total_input_tokens" : gorm .Expr ("total_input_tokens + ?" , in ),
4457+ "total_cached_input_tokens" : gorm .Expr ("total_cached_input_tokens + ?" , cin ),
4458+ "total_output_tokens" : gorm .Expr ("total_output_tokens + ?" , out ),
4459+ "latest_token_count_input_tokens" : 0 ,
4460+ "latest_token_count_cached_input_tokens" : 0 ,
4461+ "latest_token_count_output_tokens" : 0 ,
4462+ "latest_token_count_total_tokens" : 0 ,
4463+ "latest_token_count_updated_at" : nil ,
4464+ "updated_at" : time .Now (),
44244465 }
44254466}
44264467
@@ -4429,11 +4470,16 @@ func contextEstimateBaselineResetUpdate(record tables.WebSessionTable, timestamp
44294470 timestamp = time .Now ()
44304471 }
44314472 return map [string ]any {
4432- "context_baseline_input_tokens" : record .TotalInputTokens ,
4433- "context_baseline_cached_input_tokens" : record .TotalCachedInputTokens ,
4434- "context_baseline_output_tokens" : record .TotalOutputTokens ,
4435- "last_context_compaction_at" : timestamp ,
4436- "updated_at" : time .Now (),
4473+ "context_baseline_input_tokens" : record .TotalInputTokens ,
4474+ "context_baseline_cached_input_tokens" : record .TotalCachedInputTokens ,
4475+ "context_baseline_output_tokens" : record .TotalOutputTokens ,
4476+ "last_context_compaction_at" : timestamp ,
4477+ "latest_token_count_input_tokens" : 0 ,
4478+ "latest_token_count_cached_input_tokens" : 0 ,
4479+ "latest_token_count_output_tokens" : 0 ,
4480+ "latest_token_count_total_tokens" : 0 ,
4481+ "latest_token_count_updated_at" : nil ,
4482+ "updated_at" : time .Now (),
44374483 }
44384484}
44394485
@@ -5194,6 +5240,9 @@ func extractContextCompactionText(item map[string]any) string {
51945240 if text := strings .TrimSpace (stringValue (item ["text" ])); text != "" {
51955241 sections = append (sections , text )
51965242 }
5243+ if message := strings .TrimSpace (stringValue (item ["message" ])); message != "" {
5244+ sections = append (sections , message )
5245+ }
51975246 if content := strings .TrimSpace (strings .Join (collectReasoningFragments (item ["content" ]), "" )); content != "" {
51985247 sections = append (sections , content )
51995248 }
0 commit comments