modscode JA

modscode / Code / Keep state with $.store

Keep state with $.store

$.store is a key-value store every session on the machine shares (4 MiB of JSON in all). get(key) gives the value (nothing for a key never set); set(key, value) takes any JSON value. Keep values several sessions change in $.store rather than in a file ($.fs).

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

The smallest form

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)
  })
}

Written by modscode with the API of the official docs. Free to use.

Real mods' code (6)

Quoted as is from public mods on GitHub, at the commit the gallery read. The heading leads to the mod's page (what it does, its good points, its design, what its code reaches); the file name leads to the lines on GitHub.

Check the code yourself before you copy or install it. Mods run unsandboxed, with your permissions. modscode does not vouch for what the code does.

intermission Jarrod Watts · Terminal + desktop · ★ 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 licence. Keep its notice when you copy the code. Its code and design

Licence text (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 · Terminal + desktop · ★ 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 licence. Keep its notice when you copy the code. Its code and design

Licence text (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 · Terminal · ★ 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 licence. Keep its notice when you copy the code. Its code and design

Licence text (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 · Terminal + desktop · ★ 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 licence. Keep its notice when you copy the code. Its code and design

Licence text (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 · Terminal + desktop · ★ 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 licence. Keep its notice when you copy the code. Its code and design

Licence text (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 · Terminal + desktop · ★ 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 licence. Keep its notice when you copy the code. Its code and design

Licence text (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.

Start building

Put a .claude-plugin/plugin.json ({"name": "my-mod"}) and a hooks/hooks.json ({"modules": ["register.js"]}) in a folder, save the code above as hooks/register.js, and run claude --plugin-dir . to load it for one session. claude plugin validate . lists the events it hooks and the APIs it calls.

Other patterns