Scope kanban and usage profile reads

This commit is contained in:
ekko
2026-05-24 08:46:49 +08:00
committed by ekko
parent be2089e423
commit f372d0a905
5 changed files with 99 additions and 12 deletions
@@ -4,6 +4,7 @@ import { resolve, normalize } from 'path'
import { homedir } from 'os'
import * as kanbanCli from '../../services/hermes/hermes-kanban'
import { isPathWithin } from '../../services/hermes/hermes-path'
import { listProfileNamesFromDisk } from '../../services/hermes/hermes-profile'
import {
searchSessionSummariesWithProfile,
getSessionDetailFromDbWithProfile,
@@ -29,8 +30,6 @@ function allowedProfileSet(ctx: Context): Set<string> | null {
}
function visibleProfileSet(ctx: Context): Set<string> | null {
const profile = requestedProfile(ctx)
if (profile) return new Set([profile])
return allowedProfileSet(ctx)
}
@@ -67,10 +66,26 @@ function statsForTasks(tasks: kanbanCli.KanbanTask[]): kanbanCli.KanbanStats {
return { by_status, by_assignee, total: tasks.length }
}
function filterAssigneesByVisibleProfiles(ctx: Context, assignees: kanbanCli.KanbanAssignee[]): kanbanCli.KanbanAssignee[] {
const visible = visibleProfileSet(ctx)
if (!visible) return assignees
return assignees.filter(assignee => visible.has(profileName(assignee.name)))
function assignableProfileNames(ctx: Context): Set<string> | null {
const user = ctx.state?.user
if (!user) return null
if (user.role === 'super_admin') return new Set(listProfileNamesFromDisk())
return new Set(listUserProfiles(user.id).map(profile => profile.profile_name))
}
function assigneesForUser(ctx: Context, assignees: kanbanCli.KanbanAssignee[]): kanbanCli.KanbanAssignee[] {
const assignable = assignableProfileNames(ctx)
if (!assignable) return assignees
const byName = new Map<string, kanbanCli.KanbanAssignee>()
for (const assignee of assignees) {
const name = profileName(assignee.name)
if (assignable.has(name)) byName.set(name, { ...assignee, name })
}
for (const name of [...assignable].sort()) {
if (!byName.has(name)) byName.set(name, { name, on_disk: true, counts: null })
}
return [...byName.values()]
}
async function getVisibleTasksForBoard(ctx: Context, board: string, opts: {
@@ -671,7 +686,7 @@ export async function assignees(ctx: Context) {
const board = requestBoard(ctx)
if (!board) return
try {
const assignees = filterAssigneesByVisibleProfiles(ctx, await kanbanCli.getAssignees({ board }))
const assignees = assigneesForUser(ctx, await kanbanCli.getAssignees({ board }))
ctx.body = { assignees }
} catch (err: any) {
ctx.status = 500
@@ -428,6 +428,7 @@ export async function contextLength(ctx: any) {
export async function usageStats(ctx: any) {
const rawDays = parseInt(String(ctx.query?.days ?? '30'), 10)
const days = Number.isFinite(rawDays) && rawDays > 0 ? Math.min(rawDays, 365) : 30
const profile = requestedProfile(ctx)
let hermes = {
input_tokens: 0,
@@ -443,7 +444,7 @@ export async function usageStats(ctx: any) {
}
try {
hermes = await getUsageStatsFromDb(days)
hermes = profile ? await getUsageStatsFromDb(days, undefined, profile) : await getUsageStatsFromDb(days)
} catch (err) {
logger.warn(err, 'usageStats: failed to load Hermes usage analytics from state.db')
}
+4 -3
View File
@@ -572,12 +572,12 @@ function chainOrderSql(ids: string[]): string {
return ids.map((_, index) => `WHEN ? THEN ${index}`).join(' ')
}
async function openSessionDb() {
async function openSessionDb(profile?: string) {
if (!SQLITE_AVAILABLE) {
throw new Error(`node:sqlite requires Node >= 22.5, current: ${process.versions.node}`)
}
const { DatabaseSync } = await import('node:sqlite')
const dbPath = sessionDbPath()
const dbPath = profile ? join(getProfileDir(profile), 'state.db') : sessionDbPath()
try {
return new DatabaseSync(dbPath, { open: true, readOnly: true })
} catch (err: any) {
@@ -1104,6 +1104,7 @@ export async function getSkillUsageStatsFromDb(
export async function getUsageStatsFromDb(
days = 30,
nowSeconds = Math.floor(Date.now() / 1000),
profile?: string,
): Promise<HermesUsageStats> {
const empty: HermesUsageStats = {
input_tokens: 0,
@@ -1121,7 +1122,7 @@ export async function getUsageStatsFromDb(
const normalizedDays = Number.isFinite(days) ? days : 30
const safeDays = Math.max(1, Math.floor(normalizedDays))
const since = nowSeconds - safeDays * 24 * 60 * 60
const db = await openSessionDb()
const db = await openSessionDb(profile)
try {
const apiCallsExpr = tableHasColumn(db, 'sessions', 'api_call_count')