Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/opencode/src/cli/cmd/tui/routes/session/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import path from "path"
import type { AssistantMessage } from "@opencode-ai/sdk/v2"
import { Global } from "@/global"
import { Installation } from "@/installation"
import { useKeybind } from "../../context/keybind"
import { useDirectory } from "../../context/directory"
import { useKV } from "../../context/kv"
import { TodoItem } from "../../component/todo-item"
Expand Down Expand Up @@ -51,15 +50,25 @@ export function Sidebar(props: { sessionID: string }) {
const context = createMemo(() => {
const last = messages().findLast((x) => x.role === "assistant" && x.tokens.output > 0) as AssistantMessage
if (!last) return
const total =
last.tokens.input + last.tokens.output + last.tokens.reasoning + last.tokens.cache.read + last.tokens.cache.write
const total = last.tokens.input + last.tokens.output
const model = sync.data.provider.find((x) => x.id === last.providerID)?.models[last.modelID]
const limit = model?.limit.context ?? 0
return {
tokens: total.toLocaleString(),
percentage: model?.limit.context ? Math.round((total / model.limit.context) * 100) : null,
percentage: limit ? Math.round((total / limit) * 100) : null,
limit: limit ? (limit >= 1000000 ? `${(limit / 1000000).toFixed(1)}M` : `${Math.round(limit / 1000)}k`) : null,
}
})

const sessionTokens = createMemo(() => {
const total = messages().reduce((sum, msg) => {
if (msg.role !== "assistant") return sum
const assistant = msg as AssistantMessage
return sum + assistant.tokens.input + assistant.tokens.output
}, 0)
return total.toLocaleString()
})

const directory = useDirectory()
const kv = useKV()

Expand Down Expand Up @@ -93,7 +102,15 @@ export function Sidebar(props: { sessionID: string }) {
<b>Context</b>
</text>
<text fg={theme.textMuted}>{context()?.tokens ?? 0} tokens</text>
<text fg={theme.textMuted}>{context()?.percentage ?? 0}% used</text>
<text fg={theme.textMuted}>
{context()?.percentage ?? 0}% used{context()?.limit ? ` of ${context()!.limit}` : ""}
</text>
</box>
<box>
<text fg={theme.text}>
<b>Session</b>
</text>
<text fg={theme.textMuted}>{sessionTokens()} total tokens</text>
<text fg={theme.textMuted}>{cost()} spent</text>
</box>
<Show when={mcpEntries().length > 0}>
Expand Down