modscode EN

modscode / コード参考書 / $.store で状態を残す

$.store で状態を残す

$.store は、同じ PC のすべてのセッションが共有するキーと値の置き場です(JSON で合わせて 4 MiB まで)。get(key) は値(無いキーなら空)、set(key, value) は JSON にできる値を受けます。いくつものセッションが書く値は、ファイル($.fs)より $.store に置きます。

$.store.get$.store.set$.store.delete$.store.keys

最小の形

hooks/register.js
let count = 0

export function register(on) {
  on('session.start', async ($, e, next) => {
    // every session on this machine shares $.store
    count = Number((await $.store.get('turns')) ?? 0)
    return next(e)
  })

  on('turn.complete', async ($, e, next) => {
    count += 1
    await $.store.set('turns', count)
    $.ui.status(count + ' turns so far')
    return next(e)
  })
}

modscode が公式ドキュメントの API で書いた最小の例です。自由に使えます。

実際の Mod のコード(6 本)

GitHub の公開 Mod から、図鑑が読んだコミットの該当行をそのまま引いています。見出しから Mod のページ(何をするか・良いところ・画面・コードが触るもの)へ、ファイル名から GitHub の該当行へ移れます。

写す前・入れる前に、必ず自分でコードを確かめてください。Mod はサンドボックスなしで、あなたの権限で動きます。modscode は中身の安全を保証しません。

intermission Jarrod Watts · ターミナル+デスクトップ · ★ 99

Play Doom deathmatch while Claude works, and get handed back when it's done

on('session.start', async ($, e, next) => {
  isOn = (await $.store.get('isOn')) === true
  name = await $.store.get('name')
  if (!name) {
    name = randomName()
    await $.store.set('name', name)
  }
  await $.command.register({
    name: 'intermission',
    description: 'Play Doom while Claude works',
    argumentHint: '[off]',
  })
  return next(e)
})

MIT ライセンス。写すときは著作権表示を残してください。 この Mod のコードと画面

ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 Jarrod Watts

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

compact-adviser Kun Chen · ターミナル+デスクトップ · ★ 197

Suggests (or, opt-in, runs) /compact at a completed checkpoint, judged by TypeSafe Jev. Its hooks module activates only when CLAUDE_CODE_ENABLE_FUNCTION_HOOKS is exactly 1 and is otherwise a complete no-op.

on("session.compact", async ($, e, next) => {
  const result = await next(e);
  if (!(await isActivated($)) || !interactive) return result;
  if (e.trigger === "precompute" || e.agentId !== undefined || result.skip !== undefined) {
    return result;
  }
  try {
    await invalidate($);
    const key = sessionKey(await $.session.id());
    await $.store.set(key, initialState(true, await $.clock.now()));
  } catch {
    // The cooldown record stays as it was; the next judgment re-reads fresh usage.
  }
  return result;
});

MIT ライセンス。写すときは著作権表示を残してください。 この Mod のコードと画面

ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 kunchenguid

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

snake Hamza Zafar · ターミナル · ★ 120

Snake in a pane while Claude works, paused when it's done. Opt-in, so nothing opens until /snake.

on('ui.message', async ($, e) => {
  const data = e.data as { score?: unknown }
  if (e.element !== 'snake' || typeof data?.score !== 'number') return {}
  const now = data.score
  await update($, score, () => now)
  const before = await read($, best)
  if (now > before) {
    await update($, best, () => now)
    await $.store.set(BEST_KEY, now)
  }
  return {}
})

MIT ライセンス。写すときは著作権表示を残してください。 この Mod のコードと画面

ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 Hamza Zafar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

session-saver Hamza Zafar · ターミナル+デスクトップ · ★ 120

Names untitled sessions through unpause. /park saves where you left off, and a resumed session shows it.

on('turn.complete', async ($, e, next) => {
  const r = await next(e)
  if (!e.agentId) {
    const id = await $.session.id()
    // Without a /park, a resume still shows the last thing asked.
    if (prompt) {
      const last: Parked = { leftOff: `you asked: ${oneLine(prompt, 80)}`, next: '', note: '', at: Date.now() }
      await $.store.set(`last:${id}`, last)
    }
    if ((await $.session.turns()) >= NAME_AFTER_TURNS && !(await $.store.get(`named:${id}`))) {
      await $.store.set(`named:${id}`, true) // one try per session, whatever happens
      void autoname($, id).catch(() => {})
    }
  }
  return r
})

MIT ライセンス。写すときは著作権表示を残してください。 この Mod のコードと画面

ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 Hamza Zafar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

cortex-bar Cortex Hub · ターミナル+デスクトップ · ★ 58

Shows Cortex Hub tool calls above the prompt as a stacked bar, one color per category, with /cs session progress, tokens in and saved, and hit@k for code lookups. Toggle with /cortex-bar, per-tool table and calls with /cortex-calls.

on('command.run', { command: ['cortex-bar', 'cortex-calls'] }, async ($, e) => {
  if (e.command === 'cortex-calls') return openCalls($)
  const arg = e.args.trim().toLowerCase()
  if (arg === 'reset') {
    resetCalls()
    $.ui.invalidate('ui.render')
    return { text: 'cortex-bar: call counts cleared' }
  }
  if (arg !== '' && arg !== 'on' && arg !== 'off') {
    return { text: 'usage: /cortex-bar [on|off|reset]' }
  }
  visible = arg === '' ? !visible : arg === 'on'
  await $.store.set('visible', visible)
  $.ui.invalidate('ui.render')
  return { text: visible ? 'cortex-bar: shown' : 'cortex-bar: hidden (/cortex-bar shows it)' }
})

MIT ライセンス。写すときは著作権表示を残してください。 この Mod のコードと画面

ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 Cortex Hub Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

lemo-meter lemomo · ターミナル+デスクトップ · ★ 16

用量横条:输入框上方显示上下文、5 小时额度、花费和 git 分支;状态栏显示消息编号(已有自己的状态栏时默认关闭);点「检查」才联网查新版本 / Usage meter: context, 5-hour quota, cost and git branch above the prompt; a status line with the message number (off by default if you have your own); checks for upd…

on('lemo.toggle', async ($, e, next) => {
  if (e.mod !== 'lemo-meter') return next(e)
  const want = e.on
  if (want === undefined) return { value: false }
  if (e.id === 'band') {
    await update($, bandOn, () => want)
    await $.store.set('band', want)
  } else if (e.id === 'status') {
    await update($, statusPref, () => want)
    await $.store.set('status', want)
    await showStatus($)
  } else {
    return { value: false }
  }
  return { value: true }
})

MIT ライセンス。写すときは著作権表示を残してください。 この Mod のコードと画面

ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 lemomo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

作り始める

フォルダーに .claude-plugin/plugin.json({"name": "my-mod"})と hooks/hooks.json({"modules": ["register.js"]})を置き、上のコードを hooks/register.js に保存して、claude --plugin-dir . で 1 セッションだけ読み込みます。claude plugin validate . で、フックするイベントと呼ぶ API の一覧が出ます。

ほかのパターン