mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(desktop): show session tokens and cost on the session view (#53785)
Co-authored-by: LukeParkerDev <10430890+Hona@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
46e4695b96
commit
7b33bcea4e
@@ -29,6 +29,7 @@ import type {
|
||||
SessionMessageAssistantText,
|
||||
SessionMessageAssistantTool,
|
||||
SessionInfo,
|
||||
SessionListInput,
|
||||
SessionInboxInfo,
|
||||
SessionInboxCompaction,
|
||||
ShellInfo,
|
||||
@@ -204,6 +205,8 @@ function createSync() {
|
||||
}
|
||||
}
|
||||
|
||||
const CHILD_PAGE = 200
|
||||
|
||||
export function createData(config: CreateDataInput) {
|
||||
const api = config.api
|
||||
let disposed = false
|
||||
@@ -499,6 +502,16 @@ export function createData(config: CreateDataInput) {
|
||||
return created
|
||||
}
|
||||
|
||||
// Every direct subagent, not only the server's default first page, so a root's family cost
|
||||
// counts all of them. Pages stay newest first, the order the family index has always used.
|
||||
async function listChildren(input: SessionListInput): Promise<SessionInfo[]> {
|
||||
const page = await api().session.list(input)
|
||||
|
||||
if (page.data.length < CHILD_PAGE || !page.cursor.next) return page.data
|
||||
|
||||
return [...page.data, ...(await listChildren({ cursor: page.cursor.next, limit: CHILD_PAGE }))]
|
||||
}
|
||||
|
||||
// Walk parentID upward through loaded session info to the family root. When a
|
||||
// parent's info is missing, that missing ID is the furthest-known ancestor and
|
||||
// is returned so orphan subtrees group under it until the parent arrives. A
|
||||
@@ -1619,11 +1632,7 @@ export function createData(config: CreateDataInput) {
|
||||
return sync.run(options?.children ? `session.family:${sessionID}` : `session:${sessionID}`, async () => {
|
||||
const [info, children] = await Promise.all([
|
||||
api().session.get({ sessionID }),
|
||||
options?.children
|
||||
? api()
|
||||
.session.list({ parentID: sessionID, order: "desc" })
|
||||
.then((response) => response.data)
|
||||
: [],
|
||||
options?.children ? listChildren({ parentID: sessionID, order: "desc", limit: CHILD_PAGE }) : [],
|
||||
])
|
||||
const sessions = [info, ...children]
|
||||
batch(() => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./data"
|
||||
export * from "./connection"
|
||||
export * from "./pty"
|
||||
export * from "./usage"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { SessionMessageAssistant, SessionMessageInfo } from "../promise"
|
||||
|
||||
/**
|
||||
* The assistant step whose tokens fill the context window now: the latest one after the last completed
|
||||
* compaction and before the revert boundary. Every client shows context usage from this step, so the TUI
|
||||
* and the desktop app agree on it.
|
||||
*/
|
||||
export function lastAssistantWithUsage(messages: ReadonlyArray<SessionMessageInfo>, boundary?: string) {
|
||||
const boundaryIndex = boundary ? messages.findIndex((message) => message.id === boundary) : -1
|
||||
|
||||
if (boundary && boundaryIndex === -1) return undefined
|
||||
const end = boundaryIndex === -1 ? messages.length : boundaryIndex
|
||||
|
||||
const compactionIndex = messages.findLastIndex(
|
||||
(message, index) => message.type === "compaction" && message.status === "completed" && index < end,
|
||||
)
|
||||
|
||||
return messages.findLast(
|
||||
(message, index): message is SessionMessageAssistant & { tokens: NonNullable<SessionMessageAssistant["tokens"]> } =>
|
||||
message.type === "assistant" && message.tokens !== undefined && index > compactionIndex && index < end,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user