Clears the session when the current turn ends, then submits a prompt you or Claude gave

Mods (Claude Code plugins built on function hooks) made by the OrbitMap team. They work in Claude Code only, on versions that support mods. The mod API is in early access, so a Claude Code update can change how a mod behaves.
At the prompt of a Claude Code terminal session:
/plugin install <mod> --marketplace Orbitmap/claude-mods
Answer y to add the marketplace, then pick a scope (user scope makes the mod available in every project).
Starts a fresh conversation once the current work is done.
/clear-then <prompt>: type it any time, also while Claude is working. When the running turn ends, the session is cleared (/clear) and <prompt> is submitted as the first message of the new conversation.The new conversation sees nothing from the old one, so the prompt has to be self-contained. An interrupted turn keeps the prompt waiting for the next turn that ends normally.
/plugin install clear-then --marketplace Orbitmap/claude-mods
MIT
hooks/register.ts 69 lines1import type { EngineInterface, Register } from 'claude-code'
2
3const TOOL = 'mcp__clear-then__schedule'
4
5// The prompt to submit after the next /clear; one at a time, the latest wins.
6let pending: string | undefined
7
8function clearAndSubmit($: EngineInterface, text: string) {
9 pending = undefined
10 $.ui.status(undefined)
11 $.clock.after(50, async () => {
12 await $.command.run({ command: 'clear', args: '' })
13 await $.prompt.submit({ text, asUser: true })
14 })
15}
16
17export const register: Register = on => {
18
19 on('session.start', async ($, e, next) => {
20 await $.command.register({
21 name: 'clear-then',
22 description: 'When the current turn ends, run /clear and submit the given prompt',
23 argumentHint: '<prompt>',
24 })
25 await $.tool.register({
26 name: 'schedule',
27 description:
28 'Schedules a fresh start: when your current turn ends, the session is cleared (/clear) and `prompt` is submitted as the first message of the new conversation. Use it when the user asks you to clear the session at the end of the work and continue with a given prompt (e.g. "create the task, then clear and say: do task TS-123"). The new conversation sees nothing from this one, so the prompt must be self-contained. Calling it again replaces the scheduled prompt; an empty prompt cancels it.',
29 inputSchema: {
30 type: 'object',
31 properties: { prompt: { type: 'string', description: 'The prompt to submit after /clear' } },
32 required: ['prompt'],
33 },
34 isDeferred: false,
35 })
36 return next(e)
37 })
38
39 // Typed by the person: a non-immediate command waits for the running turn to end,
40 // so by the time it runs the work is done and the session is idle.
41 on('command.run', { command: 'clear-then' }, async ($, e) => {
42 const text = e.args.trim()
43 if (text === '') return { text: 'Usage: /clear-then <prompt>' }
44 clearAndSubmit($, text)
45 return { text: `Clearing the session, then submitting: ${text}` }
46 })
47
48 // Called by Claude: remembered until the turn ends.
49 on('tool.call', { tool: TOOL }, async ($, e) => {
50 const text = String((e as { prompt?: unknown }).prompt ?? '').trim()
51 if (text === '') {
52 pending = undefined
53 $.ui.status(undefined)
54 return { result: 'Cancelled the scheduled clear.', text: 'Cancelled the scheduled clear.' }
55 }
56 pending = text
57 $.ui.status(`clear-then: ${text.slice(0, 60)}`)
58 const answer = `Scheduled: when this turn ends the session is cleared and this prompt is submitted: ${text}`
59 return { result: answer, text: answer }
60 })
61
62 on('turn.complete', async ($, e, next) => {
63 const done = await next(e)
64 // Main loop only, and only a turn that finished normally: an interrupt keeps it pending.
65 if (e.agentId === undefined && e.reason === 'answer' && pending !== undefined) clearAndSubmit($, pending)
66 return done
67 })
68}
69