modscode EN

modscode / 図鑑 / branch-usage

branch-usage

Tracks tokens and cost per model, agent and session for every session on a git branch, in a right pane.

確かめやすいコードターミナル+デスクトップペインバンドトーストBoxLinkText

何をするか

  • プロンプトの上の帯・欄・トーストを描く
  • /branch-usage を足す
  • Bash の呼び出しを見張る
  • classic.SessionStart・session.measure・session.start・turn.start ほか 1 個にフックする
  • プロセスを起こす

良いところ

  • ネットワークに出ない
  • TypeScript で書かれている
  • エラーを扱う

作り始めるためのコード

2 種類あります。上のデザインから modscode が書いた雛形と、その Mod 自身のコードのうち描いている部分です。

雛形(register.tsx)

register.tsx
import type { Register } from 'claude-code'

// Drawn like branch-usage by gpr (pane, band, toast), as modscode read it from the mod's code:
// https://modscode.com/gallery/gpr-branch-usage/
// Written by modscode from that reading, not copied from the mod: use it as you like.
// Each value below stands for one the mod works out at run time; put in your own.
export const register: Register = on => {
  // the band above the prompt; what else goes there is drawn under it
  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
    const { Box, Text, Link } = $.ui.resolve(e)
    const branch = '…'
    const url = '…'
    const number = '…'
    return (
      <Box flexDirection="column">
        <Box>
          <Text dimColor>
            0
            {" · "}
            {branch}
            {" "}
          </Text>
          <Link href={`${url}`}>
            <Text color="blue" underline>
              (#
              {number}
              )
            </Text>
          </Link>
        </Box>
        {await next(e)}
      </Box>
    )
  })

  // the pane: open it with $.ui.open({ id: 'my-pane', title: "Branch usage" })
  on('ui.render', { component: 'Pane', requestId: 'my-pane' }, async ($, e) => {
    const { Box, Text, Link } = $.ui.resolve(e)
    const branch = '…'
    const url = '…'
    const number = '…'
    const length = '…'
    const formatUsd = '…'
    const shortModel = '…'
    const sharePct = '…'
    const formatUsd2 = '…'
    const tokenLine = '…'
    const sharePct2 = '…'
    const formatUsd3 = '…'
    const tokenLine2 = '…'
    const sharePct3 = '…'
    const formatUsd4 = '…'
    const tokenLine3 = '…'
    const agent = '…'
    const requests = '…'
    const sharePct4 = '…'
    const formatUsd5 = '…'
    const tokenLine4 = '…'
    const sharePct5 = '…'
    const formatUsd6 = '…'
    const tokenLine5 = '…'
    const sharePct6 = '…'
    const formatUsd7 = '…'
    const tokenLine6 = '…'
    const sessionId = '…'
    const relativeTime = '…'
    const value = '…'
    const formatUsd8 = '…'
    const value2 = '…'
    const formatUsd9 = '…'
    const value3 = '…'
    const formatUsd10 = '…'
    return (
      <Box flexDirection="column">
        <Box flexDirection="row" justifyContent="space-between">
          <Box flexShrink={1}>
            <Box flexShrink={1}>
              <Text wrap="truncate-end" bold>
                {branch}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {" "}
              </Text>
              <Link href={`${url}`}>
                <Text color="blue" underline>
                  (#
                  {number}
                  )
                </Text>
              </Link>
            </Box>
          </Box>
          <Box flexShrink={0} marginLeft={1}>
            <Text bold>
              0
            </Text>
          </Box>
        </Box>
        <Text dimColor>
          {"  "}
          {length}
          {" session"}
        </Text>
        <Text dimColor>
          {" this session, engine total "}
          {formatUsd}
        </Text>
        <Box marginTop={1}>
          <Text bold>
            By model
          </Text>
        </Box>
        <Box flexDirection="column">
          <Box flexDirection="row" justifyContent="space-between">
            <Box flexShrink={1}>
              <Text wrap="truncate-end">
                {`  - ${shortModel} ~`}
              </Text>
            </Box>
            <Box flexShrink={0} marginLeft={1}>
              <Text dimColor>
                {sharePct}
                {" "}
              </Text>
              <Text>
                {formatUsd2}
              </Text>
            </Box>
          </Box>
          <Text dimColor wrap="truncate-end">
            {" "}
            {tokenLine}
          </Text>
        </Box>
        <Box flexDirection="column">
          <Box flexDirection="row" justifyContent="space-between">
            <Box flexShrink={1}>
              <Text wrap="truncate-end">
                {`  - ${shortModel} ~`}
              </Text>
            </Box>
            <Box flexShrink={0} marginLeft={1}>
              <Text dimColor>
                {sharePct2}
                {" "}
              </Text>
              <Text>
                {formatUsd3}
              </Text>
            </Box>
          </Box>
          <Text dimColor wrap="truncate-end">
            {" "}
            {tokenLine2}
          </Text>
        </Box>
        <Box flexDirection="column">
          <Box flexDirection="row" justifyContent="space-between">
            <Box flexShrink={1}>
              <Text wrap="truncate-end">
                {`  - ${shortModel} ~`}
              </Text>
            </Box>
            <Box flexShrink={0} marginLeft={1}>
              <Text dimColor>
                {sharePct3}
                {" "}
              </Text>
              <Text>
                {formatUsd4}
              </Text>
            </Box>
          </Box>
          <Text dimColor wrap="truncate-end">
            {" "}
            {tokenLine3}
          </Text>
        </Box>
        <Box marginTop={1}>
          <Text bold>
            By agent
          </Text>
        </Box>
        <Box flexDirection="column">
          <Box flexDirection="row" justifyContent="space-between">
            <Box flexShrink={1}>
              <Text wrap="truncate-end">
                {`  - ${agent} · ${requests} req`}
              </Text>
            </Box>
            <Box flexShrink={0} marginLeft={1}>
              <Text dimColor>
                {sharePct4}
                {" "}
              </Text>
              <Text>
                {formatUsd5}
              </Text>
            </Box>
          </Box>
          <Text dimColor wrap="truncate-end">
            {" "}
            {tokenLine4}
          </Text>
        </Box>
        <Box flexDirection="column">
          <Box flexDirection="row" justifyContent="space-between">
            <Box flexShrink={1}>
              <Text wrap="truncate-end">
                {`  - ${agent} · ${requests} req`}
              </Text>
            </Box>
            <Box flexShrink={0} marginLeft={1}>
              <Text dimColor>
                {sharePct5}
                {" "}
              </Text>
              <Text>
                {formatUsd6}
              </Text>
            </Box>
          </Box>
          <Text dimColor wrap="truncate-end">
            {" "}
            {tokenLine5}
          </Text>
        </Box>
        <Box flexDirection="column">
          <Box flexDirection="row" justifyContent="space-between">
            <Box flexShrink={1}>
              <Text wrap="truncate-end">
                {`  - ${agent} · ${requests} req`}
              </Text>
            </Box>
            <Box flexShrink={0} marginLeft={1}>
              <Text dimColor>
                {sharePct6}
                {" "}
              </Text>
              <Text>
                {formatUsd7}
              </Text>
            </Box>
          </Box>
          <Text dimColor wrap="truncate-end">
            {" "}
            {tokenLine6}
          </Text>
        </Box>
        <Box marginTop={1}>
          <Text bold>
            By session
          </Text>
        </Box>
        <Box flexDirection="row" justifyContent="space-between">
          <Box flexShrink={1}>
            <Text wrap="truncate-end">
              {`  - ● ${sessionId}  ${relativeTime}`}
            </Text>
          </Box>
          <Box flexShrink={0} marginLeft={1}>
            <Text dimColor>
              {value}
              {" "}
            </Text>
            <Text>
              {formatUsd8}
            </Text>
          </Box>
        </Box>
        <Box flexDirection="row" justifyContent="space-between">
          <Box flexShrink={1}>
            <Text wrap="truncate-end">
              {`  - ● ${sessionId}  ${relativeTime}`}
            </Text>
          </Box>
          <Box flexShrink={0} marginLeft={1}>
            <Text dimColor>
              {value2}
              {" "}
            </Text>
            <Text>
              {formatUsd9}
            </Text>
          </Box>
        </Box>
        <Box flexDirection="row" justifyContent="space-between">
          <Box flexShrink={1}>
            <Text wrap="truncate-end">
              {`  - ● ${sessionId}  ${relativeTime}`}
            </Text>
          </Box>
          <Box flexShrink={0} marginLeft={1}>
            <Text dimColor>
              {value3}
              {" "}
            </Text>
            <Text>
              {formatUsd10}
            </Text>
          </Box>
        </Box>
      </Box>
    )
  })

  on('session.start', async ($, e, next) => {
    const pr = '…'
    await $.ui.toast(`branch-usage: PR #${pr} comment created`)
    return next(e)
  })
}

Mod 自身のコード

読んだコミットにライセンスの全文が見つからなかったため、ソースの場所へのリンクだけにしています。

コードが呼ぶもの

プログラム起動ファイル書き込みClaude を動かすツール呼び出し

コードから読み取ったデザイン

読み取ったコミットのソースから描いたもので、動かした結果ではありません。一覧は 3 回描き、条件はデスクトップアプリで描く最初の分岐を取り、動かしたときにだけ決まる値は仮の例(点線の下線)にするか省きます。色と書体はこのサイトのものです。

コードから読めること

2026-10-06 に読んだコミット d1dc42c2f9fb のソースから。最初のまとまりが載せる条件で、残りは自分で判断するための材料です。後のコミットは、次に読むまで見ていません。

載せる条件: 確かめやすいコード
入口を読んだhooks/hooks.json が名指すモジュールをすべて読んだ。plugins/branch-usage/hooks/register.tsx
解析できるすべてのファイルが TypeScript か JavaScript として解析できる。2 ファイルを解析
コードが Mod の中で完結import は Mod 自身のファイルかエンジンのモジュールだけ。パッケージも require もない。コードはすべて Mod の中
読める圧縮した行・eval・new Function・計算した道の import()・符号化した塊がない。圧縮・eval・符号化した塊なし
呼び出しが見える通信とプログラム起動はその場で $.http.fetch・$.process.run と書き、他へ渡さない。外への呼び出しはすべてその場の $ で
入れる前に自分で判断する材料
描く場所コードから読んだ、描く先。ターミナル・デスクトップアプリ・両方・何も描かない(フックだけ: 見張りやプロンプトの書き換え)のどれか。出すだけで、外す理由にはしない。ターミナルとデスクトップアプリに同じように描く
入れる手順リポジトリ自身の .claude-plugin/marketplace.json にこの Mod が載っていて、ページに書いた手順でそのマーケットプレイスから入れられるか。リポジトリの .claude-plugin/marketplace.json に載っており、そこから入れられる
Windows のプログラム$.process で起動するプログラムが Windows にあるか(tail・date・open・osascript・/usr/… などは無い)。Windows にないプログラムを使わない
通信fetch の送り先(https のホスト)がコードに書いてあるか。通信なし
プログラム起動するプログラムの名前がコードに書いてあり、シェル・インタプリタ・通信用の道具でないか。git(通信する), gh(通信する)
読んで送るファイルや環境変数を読み、かつ通信するか。読んだうえで通信する
ソースの鍵ソースに API キーや秘密鍵らしきものがあるか。ソースに鍵なし
ライセンスオープンなライセンス(MIT・Apache-2.0・BSD・ISC・Unlicense・0BSD・CC0)で全文があれば、modscode にコードを載せる。違えばリンクだけ。ライセンスなし

コードの文面を読んだだけで、安全の審査ではありません。動かしてはおらず、起動するプログラムの中で何が起きるかも見えません。入れる前の確認はあなたの役目です。 載せる条件

入れる

入れる前に、必ず自分で確かめてください。Mod はサンドボックスなしで、あなたの権限で動きます。modscode はこのコードの安全を保証しません。下のコードを読み(またはフォルダーで claude plugin validate .)、触るものを見て、読んだコミットを入れてください。

リポジトリがそのままマーケットプレイスです。このマーケットプレイスを足し(ターミナルでは次のコマンド、デスクトップアプリではプラグインの設定)、そこから Mod を入れます。

/plugin marketplace add gpr/claude-code-mods
/plugin install branch-usage@claude-code-mods

入るのはマーケットプレイスの最新のコミットで、ここで読んだもの(d1dc42c2f9fb)と違うことがあります。入れる前に見比べてください。

Claude に聞く

modscode のコネクタを入れると、Claude がこのデザイン・コードが触るもの・コードを読んで、それをもとに作れます。たとえば:

modscode のデザイン「gpr-branch-usage」をコードごと読んで、同じようなペインを作って

Claude から探す: MCP コネクタ

似たデザイン