Usage data layer: types, API calls, range and aggregation

This commit is contained in:
Chris Scott authored and Forge Sandbox committed 2026-11-30 05:49:17 +00:00
1 parent 7ed857f7ea
commit 4d5a2c745e
12 files changed
+1592

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
import type {
ExperimentalSessionStatsInput,
SessionMessageAssistant,
SessionMessageInfo,
SessionStatsInfo,
} from '@opencode-manager/shared/opencode'
import { listSessionMessages } from './opencode'
import { callOpenCode } from './opencodeApi'
export const SESSION_USAGE_PAGE_LIMIT = 200
export const SESSION_USAGE_MAX_PAGES = 5
export interface SessionUsageMessages {
steps: SessionMessageAssistant[]
truncated: boolean
}
export async function getSessionStats(
input: ExperimentalSessionStatsInput,
serverId?: string,
): Promise<SessionStatsInfo> {
return callOpenCode((api) => api.experimental.session.stats(input), serverId)
}
function isAssistantMessage(message: SessionMessageInfo): message is SessionMessageAssistant {
return message.type === 'assistant'
}
export async function loadSessionUsageMessages(
sessionID: string,
serverId?: string,
): Promise<SessionUsageMessages> {
const steps: SessionMessageAssistant[] = []
let cursor: string | undefined
for (let page = 0; page < SESSION_USAGE_MAX_PAGES; page += 1) {
const result = await listSessionMessages(
sessionID,
{ limit: SESSION_USAGE_PAGE_LIMIT, type: 'assistant', cursor },
serverId,
)
steps.unshift(...result.messages.filter(isAssistantMessage))
cursor = result.nextCursor
if (cursor === undefined) {
return { steps, truncated: false }
}
}
return { steps, truncated: true }
}