feat(sessions): show token usage and estimated cost in logs (#557)

* feat(sessions): show token usage and estimated cost in logs

* fix(sessions): compact the usage footer

* fix(ci): bypass Bun cache for Windows ARM64
This commit is contained in:
Yiling-J
2026-12-01 20:49:07 +08:00
committed by GitHub
parent e3fe5792b9
commit bfeabb8244
16 changed files with 292 additions and 60 deletions
+22 -12
View File
@@ -1,5 +1,6 @@
import { zValidator } from '@hono/zod-validator'
import { agentService } from '@shumai/core/src/agent/agent'
import { agentSessionService } from '@shumai/core/src/agent/session-service'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import {
AgentInfo,
@@ -11,6 +12,7 @@ import {
} from '@shumai/dtos'
import { Hono } from 'hono'
import { HTTPException } from 'hono/http-exception'
import { z } from 'zod'
import { agentEntriesResponse } from './agent-entries'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
@@ -18,6 +20,8 @@ import type { Prisma } from '@shumai/db'
type User = Prisma.UserGetPayload<Record<string, never>>
const sessionEntriesQuerySchema = z.object({ includeUsage: z.enum(['true', 'false']).optional() })
const route = new Hono<{ Variables: { user: User } }>()
.get('/teams/:teamId/agents', async (c) => {
const teamId = c.req.param('teamId')
@@ -208,20 +212,26 @@ const route = new Hono<{ Variables: { user: User } }>()
return new Response(null, { status: 204 })
})
.get('/agent-sessions/:sessionId/entries', async (c) => {
const sessionId = c.req.param('sessionId')
const userReq = c.get('user')
.get(
'/agent-sessions/:sessionId/entries',
zValidator('query', sessionEntriesQuerySchema),
async (c) => {
const sessionId = c.req.param('sessionId')
const userReq = c.get('user')
await authzService.hasPermission({
user: userReq,
permission: Permission.Admin,
type: ResourceType.AgentSession,
id: sessionId,
})
await authzService.hasPermission({
user: userReq,
permission: Permission.Admin,
type: ResourceType.AgentSession,
id: sessionId,
})
const entries = await agentService.getSessionEntries({ sessionId })
return agentEntriesResponse(entries)
})
if (c.req.valid('query').includeUsage === 'true') {
return agentEntriesResponse(await agentSessionService.getLogs(sessionId))
}
return agentEntriesResponse(await agentService.getSessionEntries({ sessionId }))
},
)
.get('/teams/:teamId/agent-sessions', zValidator('query', paginationParamsSchema), async (c) => {
const teamId = c.req.param('teamId')
const userReq = c.get('user')